Wolfr
Wolfr

Reputation: 5164

Copy nested folders contents to one folder recursively (terminal)

I have a Wordpress upload folder that is structured using subfolders for months.

wolfr2:uploads wolfr$ tree .
.
|-- 2007
|   |-- 08
|   |   |-- beautifulkatamari.jpg
|   |   |-- beautifulkatamari.thumbnail.jpg
|   |   |-- beetle.jpg
|   |   |-- beetle.thumbnail.jpg

How do I use terminal to copy all the images recursively into another folder? I can't seem to wildcard folders like you can wildcard filenames. (e.g. *.jpg or *) (I'm on Mac OSX)

cp -R ./*.jpg .

?

Upvotes: 24

Views: 33689

Answers (3)

user2906905
user2906905

Reputation: 1

None of the above commands worked for me as such on macOS 10.15. Here's the one that worked:

find . -name "*.jpg" -exec cp /{/} [target folder path] \;

Upvotes: 0

Richard Pennington
Richard Pennington

Reputation: 19965

This will copy all *.jpg files from the current folder to a new folder and preserve the directory structure.

tar cvfp `find . -name "*.jpg"` | (cd <newfolder>; tar xfp -)

To copy without preserving the directory structure:

cp `find . -name "*.jpg"` <newfolder>

Upvotes: 38

Lucas Jones
Lucas Jones

Reputation: 20203

Off the top of my head:

find . -type f -name \*.jpg -exec cp \{\} $TARGETFOLDER \;

If that doesn't work, comment and I'll try again, but find is definitely the way to go.

Upvotes: 37

Related Questions