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.

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

An example:

/path/to/this/dir1/data.csv
/path/to/this/dir2/data.csv
/path/to/this/dir3/data.csv
/path/to/this/dir4/data.csv

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

1 Answer 1

up vote 5 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/}
done

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

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.