简体   繁体   中英

macOS - Copy all specific filename found in directories to another directory

I would like to copy all (in this case) mp4, jpg, png files which are in different directories. I would do this in macOS terminal.

For instance :

./dir11/dir2/im.png
./dir11/vi.mp4
./dir12/ima.png
./dir12/main.py
./dir12/dir2/infos.txt
./img.jpg

Result : Copy mp4, jpg and png files in found/

./found/im.png
./found/vi.mp4
./found/ima.png
./found/img.jpg

You can use find to find a list of different file names, and then pipe it via xargs to cp :

find ./looking -type f \( -name "*.mp4" -or -name "*.jpg" \) | xargs cp -t ./found

This will copy files from the folder ./looking to the folder /found ...

edit

Or in your case you probably just want to replace ./looking with ./

updated version for macOS

Since macOS cp does not support -t .

find . -type f ( -iname "*.log" -or -iname "*.jpg" ) | sed 's/ /\\ /g' | xargs -I '{}' cp '{}' ../test-out

Where:

  • The find part looks for all files (not folders) named *.log or *.jpg, and not case sensative (-iname instead of -name)
  • The sed part inserts a "\\" before any white spaces in a path
  • The xargs part uses placeholder ( {} ) to put all the arguments into the cp command.

using rsync

$ find -E . -regex '.*\\.(jpg|png|mp4)' -exec rsync -avzh {} ./found/ \\;

using cp

$ find -E . -regex '.*\\.(jpg|png|mp4)' -exec cp {} ./found/ \\;

PS - Not sure which one is more efficient. I usually prefer rsync for the copies & moves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM