LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   What's the difference between "mv dir/* /tmp" and "find dir/* -print0 | xargs" ? (https://www.linuxquestions.org/questions/linux-software-2/whats-the-difference-between-mv-dir-%2A-tmp-and-find-dir-%2A-print0-%7C-xargs-940412/)

thomas2004ch 04-18-2012 04:09 AM

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

acid_kewpie 04-18-2012 04:15 AM

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.

H_TeXMeX_H 04-18-2012 04:43 AM

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.


thomas2004ch 04-18-2012 04:53 AM

Many thanks.

Quote:

Originally Posted by H_TeXMeX_H (Post 4655966)
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.



rknichols 04-18-2012 08:27 AM

Quote:

Originally Posted by H_TeXMeX_H (Post 4655966)
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.

H_TeXMeX_H 04-18-2012 09:06 AM

Quote:

Originally Posted by rknichols (Post 4656135)
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.

rknichols 04-18-2012 01:47 PM

Quote:

Originally Posted by H_TeXMeX_H (Post 4656171)
I don't see any '-L 1' in the OP ...

"-L 1" is implied by "-I".

H_TeXMeX_H 04-18-2012 02:01 PM

Oh, yeah, I missed that. Then there is no benefit in command #2, it would be the same as using find -exec.

David the H. 04-19-2012 10:43 AM

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.


All times are GMT -5. The time now is 09:44 AM.