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

I have a file that is the same name (different data) and is located in multiple directories.

An example:


I want to copy these files (with their respective folder immediately above it) to a new location.

I tried something like this (dir1, dir2, dir3, dir4 are already made):

cp -rf /path/to/this/{dir1,dir2,dir3,di4}/data.csv ./{dir1,dir2,dir3,dir4}/.

This of course doesn't work, but I am not sure how to properly do it in one line (which would be preferred, but if a script needs to be written is not a big deal).

share|improve this question
up vote 6 down vote accepted

If your cp supports the --parents option, you can use

cd /path/to/this
cp --parents dir{1..4}/data.csv /path/to/target/

The for loop should also work:

for data in /path/to/this/dir{1..4}/data.csv ; do
    cp $data ${data#path/to/this/}

Or, use find (no brace expansion ivolved):

cd /path/to/this
find -path './dir[1234]' -exec cp {}/data.csv /path/to/target/{} \;
share|improve this answer
Your suggestions work perfectly! Thanks! – Blitztm Aug 14 '13 at 20:30

You must log in to answer this question.

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