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 documents spread between multiple directories in a given path e.g. /media/devicename/name_of_directories. I used the find command to help locate certain files e.g. .doc, docx, etc

find . -type f -regextype posix-egrep -regex '.*/.*/.*\.(doc|docx)' -exec cp -r {} somedirectory \;

I know the command cp -r is used if I am copying files from a given directory but in the case of my find command, is it necessary and if yes or no, why?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

The -r flag in cp stands for recursive. You use it when copying entire folders, because it instructs cp to descend into the folder and recursively copy everything it finds within.

You don't need to do this when you're just copying individual files, because they are (conceptually) a single entity. As Ярослав Рахматуллин says, running cp recursively on a single file doesn't make sense, because there's nothing to recurse over.

See http://en.wikipedia.org/wiki/Recursion

Or type man cp for more information.

share|improve this answer

You can not copy a file recursively, only a folder.

share|improve this answer
    
Thanks. Does that mean I don't have to use the -r switch? What would happen if I did? –  PeanutsMonkey Mar 16 '13 at 23:20
    
Using -r on a file makes no sense, thus you don't have use it. Nothing would happen. It would be copied as usual. –  Ярослав Рахматуллин Mar 16 '13 at 23:22

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.