Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .