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.

At the Linux command line, I'd like to copy a (very large) set of .txt files from one directory (and its subdirectories) to another.

I need the directory structure to stay intact, and I need to ignore files except those ending in .txt.

share|improve this question
2  
Having cp and find as tags in your question, does it mean that you're tied to these options? Since your dataset is very large, it makes sense to assume the copying process can get interrupted for some reasons and you'll have to restart it. I'm not sure the find/cp approach will be able to resume the transfer and copy only the missing part. If you aren't tied to find/cp, you could consider rsync, which is smarter. Its --exclude option will alow you to skip .txt files. –  vtest Jun 21 '11 at 8:42
    
Fair call - rsync probably is the better option. Not tied to find/cp. (I used them anyway - rsync wasn't installed on the remote machine, it was a live web server & I wanted to leave as small of a footprint as possible) –  unclaimedbaggage Jun 22 '11 at 0:08
add comment

3 Answers

up vote 31 down vote accepted

You can use find and cpio to do this

cd /top/level/to/copy
find . -name '*.txt' | cpio -pdm /path/to/destdir

(-updm for overwrite destination content.)
share|improve this answer
add comment
cd /source/path
find -type f -name \*.txt -exec install -D {} /dest/path/{} \;
share|improve this answer
add comment

how about you first copy it over with

cp -r /old/folder /new/folder

then go to the new folder and run

find . -type f ! -iname "*.txt" -delete

or just

cp -r /old/folder /new/folder && find . -type f ! -iname "*.txt" -delete

Edit: ok you want one command which filters (I have not tested this because my system doesn't have the cpio command!). Here is where I found it: http://www.gnu.org/software/findutils/manual/html_mono/find.html#Copying-A-Subset-of-Files

find . -name "*.txt" |
     cpio -pmd0 /dest-dir

Please test this first, because I haven't tried it yet. If someone would verify, that would be great.

share|improve this answer
    
nods Cheers - this would work, but without filtering to .txt I'm looking at a few million files (coming out at a few hundred GB). If need be I may have to, but I'd love to filter while copying if possible –  unclaimedbaggage Jun 21 '11 at 5:09
1  
Cheers, edited version works if I remove the '0' from -pmd0 –  unclaimedbaggage Jun 22 '11 at 0:06
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.