"In particular, the presence of \0 in your data will throw xargs for a loop (no pun intended). In this case, it is necessary to use the -0 option. This is particularly relevant if you are using the find command and supplying the -print0 option."
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.
If we're ripping this sample apart, I'd like to point out that
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?
Furthermore, parsing the output of `ls` is a terrible practice[1] that makes for an even poorer sample.
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.
If you still want to use xargs and ls perhaps avoiding \0's (perhaps so you can easily grep on the data) you can use "ls -Q | xargs rm". The advantage of that is you can still easily use grep on the data as it is still newline delimited but you won't have problems with spaces.
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.
> 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.
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.