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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
02-01-2010, 10:12 AM
|
#1
|
LQ Newbie
Registered: Jun 2007
Location: Bay Area
Distribution: RedHat, Fedora and CentOS. Since 2004
Posts: 10
Rep:
|
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
|
|
|
02-01-2010, 11:03 AM
|
#2
|
Member
Registered: Jan 2009
Location: Brisbane, Australia
Distribution: @work:RHEL 5.4/Fedora 13, @home:slack64-current,ubuntu lynx studio
Posts: 65
Rep:
|
The following may help:
Code:
for i in $(find -name '*.doc' ); do cp $i /newdir; done;
HTH
|
|
|
02-01-2010, 11:12 AM
|
#3
|
Member
Registered: Sep 2009
Location: Sparta
Posts: 237
Rep:
|
Do you need to copy (cp) or do you need to move (mv) the files?
edit: too late
|
|
|
02-01-2010, 11:27 AM
|
#4
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
02-03-2010, 10:48 AM
|
#5
|
LQ Newbie
Registered: Jun 2007
Location: Bay Area
Distribution: RedHat, Fedora and CentOS. Since 2004
Posts: 10
Original Poster
Rep:
|
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 : - ))
|
|
|
02-03-2010, 10:56 AM
|
#6
|
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083
|
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 05:57 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|