LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-10-2014, 12:56 PM   #1
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
which is preferred and why ?


Code:
find /lq -type f -exec cp '{}' /lq-bak \;
or
Code:
cp `find /lq -type f` lq-bak
?
 
Old 07-10-2014, 01:12 PM   #2
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Code:
find /lq -type f -print0 | xargs -0 cp -t lq-bak
Oh, and why, because it handles white space properly and it will be faster than the above.

Last edited by metaschima; 07-10-2014 at 01:13 PM.
 
Old 07-10-2014, 06:49 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,198

Rep: Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307
I would expect the first one to copy each file as it's found, and the second one to build the entire list then copy. So I'd prefer the first one.
 
Old 07-11-2014, 01:55 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,461

Rep: Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552Reputation: 1552
find with exec is a much more efficient way of doing it. There is a finite limt to the length of arguments passed to cp and if you have a LOT of files that match the criteria then you could exceed that limit. With find/exec this isn't an issue as each file is done "individually".
 
1 members found this post helpful.
Old 07-11-2014, 04:07 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,687

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
what about a simple cp -r /lq /lq-bak?
 
1 members found this post helpful.
Old 07-11-2014, 08:54 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by dugan View Post
I would expect the first one to copy each file as it's found, and the second one to build the entire list then copy. So I'd prefer the first one.
that was pretty much the foundation of my question so i assumed the 2nd would be prefeered because i assume copying in 1 blow is faster.

although the 1st is invoking only find it mite be less resource intensive ?
 
Old 07-11-2014, 08:56 AM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by metaschima View Post
Code:
find /lq -type f -print0 | xargs -0 cp -t lq-bak
Oh, and why, because it handles white space properly and it will be faster than the above.
[sarcasm]what idiot puts white space in filenames[/sarcasm]

is it really faster, i assume invoking find, xargs and cp would slow things down a tad ?
 
Old 07-11-2014, 08:57 AM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by pan64 View Post
what about a simple cp -r /lq /lq-bak?
the spirit of the question is about using find to create backups of certain file criteria (my example was much too simplified).

Last edited by schneidz; 07-11-2014 at 09:05 AM.
 
Old 07-11-2014, 09:43 AM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,770

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
Preferred way would be:
Code:
find /lq -type f -exec cp -t lq-bak {} +
The "+" terminator indicates that cp can accept multiple arguments, and find will invoke it with an arg list up to maximum length and use multiple invocations only if the number of arguments would exceed that. Note that use of the "-t" (--target-directory) option is required because the multiple file name arguments must be last in the argument list.

Note that these solutions do not replicate the original directory tree. All the files go directly into the target directory.

Last edited by rknichols; 07-11-2014 at 09:44 AM.
 
1 members found this post helpful.
Old 07-11-2014, 11:08 AM   #10
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by schneidz View Post
[sarcasm]what idiot puts white space in filenames[/sarcasm]

is it really faster, i assume invoking find, xargs and cp would slow things down a tad ?
Nope, it would be faster and safer than all that has been posted so far, because xargs gathers the arguments and passes them to 'cp' in batches. If you pass them one at a time as find does, it will be slower. However, if you use variable expansion like in your second example, white space will not be handled properly.

Test the speed out yourself.
 
1 members found this post helpful.
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Preferred settings for /etc/hosts NotionCommotion Linux - Networking 3 07-15-2013 10:44 PM
why is solaris preferred the_imax Solaris / OpenSolaris 18 12-22-2005 05:42 AM
Preferred hardware? Nic-MDKman Linux - Hardware 3 03-01-2004 03:59 AM
Preferred RunLevel Crashed_Again Linux - General 7 02-16-2003 12:27 AM
is runlevel 3 preferred to 5 bdp Linux - General 2 01-22-2003 03:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:50 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