LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 01-27-2013, 03:52 PM   #1
adampski
Member
 
Registered: Dec 2012
Posts: 37

Rep: Reputation: Disabled
Copying results


I want to copy all the results found from the find command, how can I do this?

Code:
find . -name index.dat | cp */index.dat dir
find . -name index.dat | cp * dir
find . -name index.dat | cp ./index.dat dir
All of the above have been non-successful
 
Old 01-27-2013, 04:51 PM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,430

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
Try this
Code:
find . -name "index.dat" -exec cp '{}' dir \;
It is a good habit to get into that you always quote the argument to the -name option to avoid expansion by the shell.
 
Old 01-27-2013, 06:10 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
One minor modification to the above:

Code:
find . -name "index.dat" -exec cp -t dir '{}' +
Using "+" at the end turns on xargs-style batch-mode processing, so it will move multiple files at once with a single command, saving you overhead and execution time.

However, using it requires that the '{}' file insertion brackets be the last argument of the command, so you also need to use the "-t" option to specify the target directory in cp/mv in order to use it properly.


Here are a couple of good links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html
 
2 members found this post helpful.
Old 01-27-2013, 08:26 PM   #4
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by allend View Post
It is a good habit to get into that you always quote the argument to the -name option to avoid expansion by the shell.
When your name is free of shell metachars like the current name of index.dat adding quotes around it is just superstition.
 
Old 01-27-2013, 08:55 PM   #5
pra838
Member
 
Registered: Feb 2010
Location: Sri Lanka
Posts: 50

Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by David the H. View Post
One minor modification to the above:

Code:
find . -name "index.dat" -exec cp -t dir '{}' +
Using "+" at the end turns on xargs-style batch-mode processing, so it will move multiple files at once with a single command, saving you overhead and execution time.

However, using it requires that the '{}' file insertion brackets be the last argument of the command, so you also need to use the "-t" option to specify the target directory in cp/mv in order to use it properly.


Here are a couple of good links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html


Above links r very useful.
Thanks
 
Old 01-27-2013, 09:00 PM   #6
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,430

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
Quote:
When your name is free of shell metachars like the current name of index.dat adding quotes around it is just superstition.
I believe the word you want is superfluous rather than superstition.

I will still argue that it is a better for someone learning to always use quotes to avoid confusing results.
 
Old 01-27-2013, 09:10 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,838

Rep: Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822Reputation: 1822
Agreed - good habits are well learnt. Otherwise known as the principle of least astonishment.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Different results in du and df? Sayan Acharjee Linux - General 3 02-29-2012 09:53 PM
Google accuses Bing of 'copying' its search results Jeebizz Linux - News 1 02-02-2011 10:05 AM
LXer: Google: Bing Is Cheating, Copying Our Search Results LXer Syndicated Linux News 3 02-02-2011 08:17 AM
[SOLVED] Copying the files inside a folder, without copying the folder (hopefully easy) tibberous Linux - Software 3 12-23-2010 02:50 AM


All times are GMT -5. The time now is 06:49 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration