Find files in subdirectories and copy them to another directory with the same subdirectories?

1

This question is very similar to this one except that I want to maintain the file's original subdirectories.

For example if I had

/temp/a/a.txt
/temp/a/a.jpg
/temp/a/b.txt
/temp/b/c.txt
/temp/d/d.txt
/temp/d/d.jpg
/temp/d/e.txt
/temp/f.txt

I'd want to copy all the text files to /temp2 so that the directory structure would look like:

/temp2/a/a.txt
/temp2/a/a.jpg
/temp2/a/b.txt
/temp2/b/c.txt
/temp2/d/d.txt
/temp2/d/d.jpg
/temp2/d/e.txt
/temp2/f.txt

Thanks for your help!!

evan

Posted 2010-11-18T00:16:52.540

Reputation: 363

Answers

0

If you want to replicate the whole directory structure, then simply do cp -a /temp /temp2 if /temp2 doesn't exist yet, or cd /temp; cp -a . /temp2 if the target directory already exists. (-a is a Linux-specific options to cp; on other systems you can use cp -Rp for a recursive copy preserving permissions.)

If you want to copy only certain files based on their names, use rsync. It has sophisticated include-exclude rules; I wrote a tutorial for these rules in response to a question asking how to do this at Unix Stack Exchange.

If you want to match files through other criteria such as the date, the simplest way is to combine rsync with zsh's pattern matching rules, as in Marcel Stimberg's answer to the above-mentioned question.

Gilles 'SO- stop being evil'

Posted 2010-11-18T00:16:52.540

Reputation: 58 319

Great, thanks for your help! Especially the links, they were just what I needed! – evan – 2010-11-18T19:06:21.440

0

Without additional information, the best solution that I can suggest is:

cp -rp temp temp2

Tok

Posted 2010-11-18T00:16:52.540

Reputation: 499