LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-01-2010, 09:12 AM   #1
w@m
LQ Newbie
 
Registered: Jun 2007
Location: Bay Area
Distribution: RedHat, Fedora and CentOS. Since 2004
Posts: 10

Rep: Reputation: 0
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
Marc
 
Old 02-01-2010, 10:03 AM   #2
bmxcess
Member
 
Registered: Jan 2009
Location: Brisbane, Australia
Distribution: @work:RHEL 5.4/Fedora 13, @home:slack64-current,ubuntu lynx studio
Posts: 65

Rep: Reputation: 19
The following may help:

Code:
for i in $(find -name '*.doc' ); do cp $i /newdir; done;
HTH
 
Old 02-01-2010, 10:12 AM   #3
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Do you need to copy (cp) or do you need to move (mv) the files?

edit: too late
 
Old 02-01-2010, 10:27 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 02-03-2010, 09:48 AM   #5
w@m
LQ Newbie
 
Registered: Jun 2007
Location: Bay Area
Distribution: RedHat, Fedora and CentOS. Since 2004
Posts: 10

Original Poster
Rep: Reputation: 0
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 : - ))
 
Old 02-03-2010, 09:56 AM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copying multiple files in multiple folders ?? stratotak Linux - Newbie 2 02-21-2009 09:41 AM
ProFTPD, one dir, multiple hd's formatking Linux - Server 1 02-21-2008 10:09 PM
Command to display /dir, /dir/sub, /dir/sub/files knockout_artist Linux - Newbie 9 10-25-2007 02:57 PM
multiple distros/one home dir/user linuxInIndy Linux - General 4 09-04-2005 09:00 PM
copy 1 file to multiple dir script carpman Linux - General 14 02-07-2002 09:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration