Re:Awesome but commonly unknown Linux Commands
by michaelcampbell on Wednesday August 10, 2011 @07:15AM
(#2867675)
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.)
[ Parent
| on HN ]
Re:Awesome but commonly unknown Linux Commands
by icebraining on Wednesday August 10, 2011 @09:50AM
(#2868151)
And too few people know that GNU Parallel[1] is most of the time a better xargs than xargs itself.
Re:Awesome but commonly unknown Linux Commands
by tedunangst on Wednesday August 10, 2011 @12:25PM
(#2868951)
How is it different than xargs -P?
Re:Awesome but commonly unknown Linux Commands
by icebraining on Wednesday August 10, 2011 @12:58PM
(#2869119)
Re:Awesome but commonly unknown Linux Commands
by tedunangst on Wednesday August 10, 2011 @03:49PM
(#2869876)
Thanks, I clicked the documentation link at the top of the page and gave up when it didn't go anywhere useful.
Re:Awesome but commonly unknown Linux Commands
by losvedir on Wednesday August 10, 2011 @08:38AM
(#2867897)
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?
Re:Awesome but commonly unknown Linux Commands
by michaelcampbell on Wednesday August 10, 2011 @10:36AM
(#2868384)
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
Re:Awesome but commonly unknown Linux Commands
by planckscnst on Wednesday August 10, 2011 @07:52AM
(#2867775)
find -exec is almost always faster and more secure than xargs.
Re:Awesome but commonly unknown Linux Commands
by burgerbrain on Wednesday August 10, 2011 @09:42AM
(#2868111)
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.
Re:Awesome but commonly unknown Linux Commands
by e1ven on Wednesday August 10, 2011 @08:37AM
(#2867894)
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.
Re:Awesome but commonly unknown Linux Commands
by michaelcampbell on Wednesday August 10, 2011 @10:37AM
(#2868391)
I'd like to hear some justification, cites, or examples for either of these assertions.
Re:Awesome but commonly unknown Linux Commands
by planckscnst on Wednesday August 10, 2011 @11:44AM
(#2868729)
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_...
Re:Awesome but commonly unknown Linux Commands
by fluidcruft on Wednesday August 10, 2011 @09:58AM
(#2868187)
Does find have an equivalent for xargs "-P max" flag?
Re:Awesome but commonly unknown Linux Commands
by tange on Thursday August 11, 2011 @07:30AM
(#2872354)
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