Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

view
 |
 +--- 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?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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