What's the difference between "mv dir/* /tmp" and "find dir/* -print0 | xargs" ?
Linux - SoftwareThis 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
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.
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.
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?
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.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.