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.

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!!

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
    
Great, thanks for your help! Especially the links, they were just what I needed! –  evan Nov 18 '10 at 19:06
add comment

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

cp -rp temp temp2
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.