Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .