LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cp help, copying multiple doc files from multiple dir to one dir. How do i do that? (https://www.linuxquestions.org/questions/linux-newbie-8/cp-help-copying-multiple-doc-files-from-multiple-dir-to-one-dir-how-do-i-do-that-786214/)

w@m 02-01-2010 09:12 AM

cp help, copying multiple doc files from multiple dir to one dir. How do i do that?
 
Hello,

I have 60+ directory's each containing multiple .doc files. I need to move them to a single directory and keep their file name intact. I don't think cp will do that with out listing all the file names. I was thinking of something like: cp -r /dir/*.doc /newdir . Or should I use a combo like find -type *.doc|cp /newdir?

What would be the best way to accomplish this?

Thank you for your help:hattip:
Marc

bmxcess 02-01-2010 10:03 AM

The following may help:

Code:

for i in $(find -name '*.doc' ); do cp $i /newdir; done;
HTH

carbonfiber 02-01-2010 10:12 AM

Do you need to copy (cp) or do you need to move (mv) the files?

edit: too late

jschiwal 02-01-2010 10:27 AM

Be careful with files that contain spaces or "evil" characters. They can break up the filename to multiple arguments.
This usually means enclosing a variable inside doublequotes.

Also be careful if you have a very large number of files. One solution is to use find with the -print0 command. Pipe the argument to "xargs -0 -L <limit> cp --target-directory /path/to/destination/"
find ./ -iname "*.doc" -print0 | xargs -0 -L 100 cp --target-directory"

For complicated operations, you can use find's -printf to print each command to run and produce a script to run afterwards.
This allows you to preview what would be done.

A common technique is to insert "echo" before a "cp", "mv" or "rm" command to verify that the command will work OK before committing to it.

w@m 02-03-2010 09:48 AM

Thank you
 
Thanks for the fast response.

Unfortunately jschiwal has a valid point. I found numerous files with long and spaced (not to mention other characters) names. These files are not getting copied. I think the safest way looks to be the manual way. I was able to copy most of the files and I will just manually copy up the rest.

Thanks for your help and saving me some hours : - ))

i92guboj 02-03-2010 09:56 AM

jschiwal's way is perfectly safe. It will use null chars as separators allowing you to correctly copy file names with spaces in the middle (or even with carriage return characters in the middle). The "echo" suggestion is also something you should take note of, for the next time.


All times are GMT -5. The time now is 10:24 AM.