Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.)


And 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/


How is it different than xargs -P?



Thanks, I clicked the documentation link at the top of the page and gave up when it didn't go anywhere useful.


I 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?


With 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


find -exec is almost always faster and more secure than xargs.


It is my understanding that -exec is almost always slower, since it generally needs to invoke whatever program you are using many many times more.


Whereas 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.


I'd like to hear some justification, cites, or examples for either of these assertions.


I 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-x... http://www.gnu.org/software/findutils/manual/html_node/find_...


Does find have an equivalent for xargs "-P max" flag?


You 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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: