-1

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
  • https://stackoverflow.com/questions/15617016/copy-all-files-with-a-certain-extension-from-all-subdirectories – chenchuk Nov 03 '18 at 07:31

2 Answers2

0

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.
code_fodder
  • 14,015
  • 12
  • 79
  • 140
0

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.

vivekyad4v
  • 11,561
  • 4
  • 46
  • 55