Find folder by name then copy files

Viewed 62

I need to find subdirectories called "child" and copy the files in those folders to a new directory. Can't get it to work.

I can get as far as finding the subdirectories but I can't copy the files from them.

> find /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/2016/03_2016 -type d -iname child

The above will find all subdirectories in 03_2016 named "child" but how do I now copy the files inside those directories?

I tried this but the problem is that it seems to want to copy the directories themselves and not just the files:

> find /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/2016/03_2016 -type d -iname child | xargs cp '{}' /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/TEST \;

I can't get it to target only the files.

2 Answers

You're close:

> find $YOUR_PATH/03_2016/ -type d -iname child | xargs -I {} find -type f {} | xargs -I {} cp {} /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/TEST

Note: You might overwrite files that share the same name at the final destination

This might be a little cleaner than the above answer:

find $(find /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/2016/03_2016 -type d -iname child | xargs) -type f -exec cp {} /Volumes/COMMON-LIC-PHOTO-1/STUDIO-COMPLETE/ISSUETRAK/TEST \;

Two calls to find, one call to xargs. Like the other answer, this also will overwrite duplicate file names.

Related