Say I've got a directory structure somewhat like this:

 +--- 0001
 +--- 231
 +--- e2341
 ... and so on

My problem is that I want to copy a set of files that have the same name (such as file_001.jpg) to another place that has an identical directory structure, i.e. it's a mirror. Is there a way to copy files of a specific name pattern from one directory structure to another?

I tried something along the lines with…

cp -R */file_001.jpg /destination_folder

…but all I get is the following error

cp: cannot stat `file_001.jpg': No such file or directory

I guess I have to pipe in the file names somehow, anyone got any good idea on how to do this?


You can use find combined with the passthrough feature of cpio for that. Something like this:

find old_dir -name "file_001.jpg" -print | cpio -pd new_dir

The -p flag on cpio triggers passthrough mode, which effectively just copies files from their original location to new_dir. The -d flag creates all the necessary directories, so you'll have the same heirarchy under new_dir as you had under old_dir (at least, so far as directories containing file_001.jpg are concerned).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.