4 ms·
xargs. Too few people know xargs. Every time I see a "find" with "-exec" in it, my soul cries a little bit. (Yes, sometimes it might be necessary, but in the
by michaelcampbell 15y ago
xargs. Too few people know xargs. Every time I see a "find" with "-exec" in it, my soul cries a little bit. (Yes, sometimes it might be necessary, but in the vast majority of cases not.)
- planckscnst 15y agofind -exec is almost always faster and more secure than xargs.
- e1ven 15y agoWhereas I almost always use for i in `find . -name foo`; do mv $i $i-new; done That's very straightforward, and gets the job done without much weirdness.
- burgerbrain 15y agoIt is my understanding that -exec is almost always slower, since it generally needs to invoke whatever program you are using many many times more.
- fluidcruft 15y agoDoes find have an equivalent for xargs "-P max" flag?
- tange 15y agoYou can emulate it with sem: find . -exec sem --id my_find -j3 sleep 4\; echo {} \; See more about sem: http://www.gnu.org/software/parallel/sem.html http://www.gnu.org/software/parallel/sem.html
- michaelcampbell 15y agoI'd like to hear some justification, cites, or examples for either of these assertions.
- planckscnst 15y agoI was mis-remembering the "faster" part. It is slower unless you have a version that supports the "-exec {} +" option. However, for security and robustness, see this: http://stackoverflow.com/questions/896808/find-exec-cmd-vs-xargs http://stackoverflow.com/questions/896808/find-exec-cmd-vs-x... http://www.gnu.org/software/findutils/manual/html_node/find_html/Security-Considerations-for-xargs.html http://www.gnu.org/software/findutils/manual/html_node/find_...
- losvedir 15y agoI thought find -exec was the preferred way of doing it (better with spaces, not limited in command line length, etc). Why would it be better to use xargs?
- michaelcampbell 15y agoWith gnu tools, at least, xargs has a "-0" option to go hand in hand with gnu find's "-print0" option which obviates the spaces problem. xargs is "better" in that it spawns 1 new process for however many things are found. -exec spawns 1 new process for EVERY thing found. So: find . -name '*.log' -exec rm {} \; spawns an rm for every log file. find . -name '*.log' -print0 | xargs -0 rm spawns 1 rm for MANY log files. (Yes, I know zsh can do stuff like this too.) xargs also has options to limit command line length or # of items if you want to limit that. find -exec is about the same then as find ... | xargs -n1
- icebraining 15y agoAnd too few people know that GNU Parallel[1] is most of the time a better xargs than xargs itself. [1]: http://www.gnu.org/s/parallel/ http://www.gnu.org/s/parallel/
- tedunangst 15y agoHow is it different than xargs -P?
- icebraining 15y agohttp://www.gnu.org/s/parallel/man.html#differences_between_xargs_and_gnu_parallel http://www.gnu.org/s/parallel/man.html#differences_between_x...
- tedunangst 15y agoThanks, I clicked the documentation link at the top of the page and gave up when it didn't go anywhere useful.