LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-18-2012, 05:09 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 537

Rep: Reputation: 33
What's the difference between "mv dir/* /tmp" and "find dir/* -print0 | xargs" ?


I use normally following command to move files:
Code:
mv dir/* /tmp/out/
But if there are a lot of files under dir/. I will got problem sometimes. So I google from internet and follwing command and works fine. Who can tell me the difference?

Code:
find dir* -print0 | xargs -0 -I {} mv {} /tmp/out
 
Old 04-18-2012, 05:15 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
xargs is a clever and subtle little tool that takes a string of input and runs it's command ones for each input. So rather than a single mv command having to execute on a HUGE list of files, which can get too long and not be possible to run. so using xargs you only mv one file at any one time.
 
1 members found this post helpful.
Old 04-18-2012, 05:43 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292
xargs breaks up arguments that would be too long into acceptable chunks. It's more efficient than using find -exec because find -exec only runs one argument at a time, while xargs runs more than one at a time by default.

Code:
       -s max-chars
              Use at most max-chars characters per command line, including the
              command  and  initial-arguments and the terminating nulls at the
              ends of the argument strings.  The largest allowed value is sys-
              tem-dependent,  and  is  calculated as the argument length limit
              for exec, less the size of your environment, less 2048 bytes  of
              headroom.   If this value is more than 128KiB, 128Kib is used as
              the default value; otherwise, the default value is the  maximum.
              1KiB is 1024 bytes.
 
Old 04-18-2012, 05:53 AM   #4
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 537

Original Poster
Rep: Reputation: 33
Many thanks.

Quote:
Originally Posted by H_TeXMeX_H View Post
xargs breaks up arguments that would be too long into acceptable chunks. It's more efficient than using find -exec because find -exec only runs one argument at a time, while xargs runs more than one at a time by default.

Code:
       -s max-chars
              Use at most max-chars characters per command line, including the
              command  and  initial-arguments and the terminating nulls at the
              ends of the argument strings.  The largest allowed value is sys-
              tem-dependent,  and  is  calculated as the argument length limit
              for exec, less the size of your environment, less 2048 bytes  of
              headroom.   If this value is more than 128KiB, 128Kib is used as
              the default value; otherwise, the default value is the  maximum.
              1KiB is 1024 bytes.
 
Old 04-18-2012, 09:27 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,362

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
Quote:
Originally Posted by H_TeXMeX_H View Post
xargs breaks up arguments that would be too long into acceptable chunks. It's more efficient than using find -exec because find -exec only runs one argument at a time, while xargs runs more than one at a time by default.
Except that the "-I" option, as in the OP's example, implies "-L 1", i.e. limiting the number of arguments to 1.

For the case of the mv command, you can use its "--target-directory=" option, which lets you place all of the source arguments at the end of the command line, eliminating the need for the "-I" option in xargs when invoking mv.
 
Old 04-18-2012, 10:06 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292
Quote:
Originally Posted by rknichols View Post
Except that the "-I" option, as in the OP's example, implies "-L 1", i.e. limiting the number of arguments to 1.

For the case of the mv command, you can use its "--target-directory=" option, which lets you place all of the source arguments at the end of the command line, eliminating the need for the "-I" option in xargs when invoking mv.
I don't see any '-L 1' in the OP, but you are right that it would limit it if it were there. I don't like the -I option either.
 
Old 04-18-2012, 02:47 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,362

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
Quote:
Originally Posted by H_TeXMeX_H View Post
I don't see any '-L 1' in the OP ...
"-L 1" is implied by "-I".
 
Old 04-18-2012, 03:01 PM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292
Oh, yeah, I missed that. Then there is no benefit in command #2, it would be the same as using find -exec.
 
Old 04-19-2012, 11:43 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,846

Rep: Reputation: 2007Reputation: 2007Reputation: 2007Reputation: 2007Reputation: 2007Reputation: 2007Reputation: 2007Reputation: 2007Reputation: 2007Reputation: 2007Reputation: 2007
If you use find's -exec option with "+" as the terminator, rather than ";", it runs in xargs-like fashion. That is, it intelligently breaks the input file list up into optimal chunks. This is a relatively recent posix addition, I believe, so it should be useable on all newer platforms.

Code:
$ time find . -type f -exec file '{}' \;
<outputs list of files>

real    0m12.090s
user    0m0.424s
sys     0m0.780s

$ time find . -type f -exec file '{}' \+
<outputs list of files>

real    0m1.125s
user    0m0.368s
sys     0m0.040s
Note though that the '{}' brackets are more limited in the xargs-style case, and must be positioned at the end of the exec'd command. Not to mention that the command itself must be capable of handling multiple files in this way.
 
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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to recover lost mp3s for free, using photorec, find, xargs and "EasyTag" GlennsPref Mandriva 2 05-12-2010 11:30 AM
newbie question: whats the difference between "su root", "su" and "su -&quo mojarron Slackware 9 12-07-2009 05:08 PM
Several "find -exec" and "find | xargs" questions thanhvn Programming 4 12-02-2005 02:04 PM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 03:32 PM
"configure: error: can not find sources in <dir>" naveenkoneru Linux - Software 0 05-31-2004 06:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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