0

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?

2

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.

1

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

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.