A small primer on xargs
bitops.io
bitops.io
This glosses over the fact that those two things are intended to be used together. Delimiting filenames with a \0 is superior to delimiting them with spaces or newlines, as filenames cannot usually contain ASCII NUL; xargs and find can therefore be used to remove heterogeneous lists of files.
Additionally, the example given may better be worded as "find . -name '* junk* -exec rm '{}' \;" or even "find . -name '* junk* | xargs rm". (as given it strikes me as a useless use of grep and is additionally fragile if ls is aliased to "ls --color=always", spewing ANSI into
EDIT: Please excuse the additional spaces in the find command arguments, they are a formatting avoidance feature.
rm *junk*
in this case would've done the same, without spawning lots of useless stuff.xargs is cool. The example fails to show a decent use case. For me a line like that would indicate that someone is _not_ familiar with his tools. Similar to
cat somefile | grep something
instead of grep something somefile
Pipes and processes aren't endangered or (in practice) limited, but we still don't need to be wasteful?A little further in the article, the author even suggests running
`ls | grep junk`
(instead of the much simpler `ls junk*`. Do I need to point that out?).I also know some people are going to retort that wasting a process or a pipe is not that bad. This discussion has been had over and over again, probably ever since Unix was invented.
In any case, the poor example chosen, as well as the flaky commands used, seriously hinder the credibility of the article.
ls | grep junk
is more similar to ls -d *junk*
You can argue the first is simpler. You can also argue more successfully that it is more general.edit: I acknowledge the points in your link. Advocates for the unix "bunch of small tools" philosophy tend to overlook its weak spots.
But there's quite a lot of nice non-standard options in the GNU userspace tools, and it would be a shame not to use them when available.
Also ls is never aliased to "ls --color=always" unless you've done that more likely is its aliased to "ls --color=auto" or similar which will work normally.
My ls (which I haven't much reconfigured) behaves very differently depending on whether it's outputting to a shell or a pipe or redirect. Not just colours, but columning and filetype indicators as well.
Actually it does not run command once for each line of input. This confused me for some time until I read the man page:
"The command line for command is built up until it reaches a system-defined limit (unless the -n and -L options are used). The specified command will be invoked as many times as necessary to use up the list of input items. In general, there will be many fewer invocations of command than there were items in the input. This will normally have significant performance benefits. Some commands can usefully be executed in parallel too; see the -P option"
Think you're looking for "-n 1" if you want that behavior..
My favorite usage of xargs if for parallel operations: cat hostlist | xargs -P 0 -n 1 -I host rsh host gzip -9 log/*.log
As for the points about a bad example, I am fully aware that it is extremely contrived. The point was to not confuse anyone with commands they don't already know. I'm wary of posting examples that necessitate people learning 4-5 new commands when their focus is learning just one.