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 is a bit of a basic question but I'm trying to copy all .doc files I find in a directory and copy them to another directory.

I know each command:

find -name '*.doc' .

and:

cp filename location 

How can I combine the two commands?

share|improve this question

5 Answers 5

up vote 3 down vote accepted
find /path/to/search -name "*.doc" -exec cp {} /path/to/copy/to \;

If there are a lot of .doc files this is your best option to avoid hitting the character limit.

share|improve this answer
    
Or equivalently, but slightly faster: find /path/to/search -name "*.doc" -exec cp -t /path/to/copy/to {} + –  Gilles Nov 7 '10 at 23:29
    
@Gil, not equivalent, and not immune to the character limit. –  John T Nov 7 '10 at 23:36
    
@John: How are they not equivalent? As for the character limit, find -exec … {} + is immune to it (it operates like xargs without the quoting issues). –  Gilles Nov 7 '10 at 23:57
    
@gil take a closer look at the man pages, or, try it out with a few hundred thousand files in a deeply nested long path. –  John T Nov 8 '10 at 0:19
    
@John: About the limit: citing POSIX: “The size of any set of two or more pathnames shall be limited such that execution of the utility does not cause the system's {ARG_MAX} limit to be exceeded.” Citing find(1) on Debian lenny: “The command line is built in much the same way that xargs builds its command lines.”. And I just tested on Debian lenny, it did break up the file names into chunks correctly. About the equivalence: again, what difference do you see? –  Gilles Nov 8 '10 at 0:28

Another possibility:

find /path/to/search -name \*.doc -print0 | xargs -0 cp --target-directory=/destination/path

This cuts down on the number of invocations of the copy command when compared to find -exec (should be noticeably faster if you have a huge number of files)

share|improve this answer
    
Slartibartfast's approach lets you use more sophisticated search/find filters with document indexing tools like recollq 'keyword1 OR keyword2 AND ext:doc' | xargs cp --target-directory=~/Photos/of\ Hobson –  hobs Jun 16 '12 at 16:04

On solaris you can use the following:

find /path/to/search -name "*.doc" -exec cp {} /destination/directory \;
share|improve this answer

Find the files owned by user kelly, and copy them to a catalog: /opt/dir

# cd (into /path to copy files) e.g. cd /opt/
# mkdir dir (where you want to copy the files)
# find / -user kelly -exec cp -rfp {} /opt/dir/ \;
share|improve this answer

To generalize you question from ".doc" to ".jpg" or other binary image files you'd need additional open source tools, like tagim and recoll.

tagim will tag your images with key words (or ratings) and then you can sort/filter them using recoll and flow the list to cp using xargs to plug them together:

recollq 'ofJoe ext:jpg' | xargs cp --target-directory=~/Photos/ofJoe
share|improve this answer

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.