
A small primer on xargs - bitops
http://bitops.io/blog/1336893229/xargs
======
DHowett
"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.

~~~
asdfaoeu
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.

~~~
robertskmiles
> 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.

------
gizzlon
_"What xargs is doing is taking each line of input and applying the supplied
argument or command to the line of input"_

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

~~~
leed25d
I was going to respond on this very point myself. This blog entry is just
plain wrong wrt the way that xargs works.

------
slashclee
xargs is so 20th-century. parallel[0] is way more awesome (especially with all
the multi-core systems everybody has now). Cool bonus: all the things you
learn for xargs work in parallel too!

[0]: <http://www.gnu.org/software/parallel/>

~~~
wwwhizz
I Was wondering what the difference with running parallel jobs using xargs -P
was, but that's covered in their man-page:
[http://www.gnu.org/software/parallel/man.html#differences_be...](http://www.gnu.org/software/parallel/man.html#differences_between_xargs_and_gnu_parallel)

------
webreac
I try to use perl instead of sed, awk, bash each time the needs are beyond
basic usage. This applies also sometimes for xargs for example with the
following command. find . -name junk\\* | perl -nle unlink

My favorite usage of xargs if for parallel operations: cat hostlist | xargs -P
0 -n 1 -I host rsh host gzip -9 log/*.log

------
djoe48
ls | grep junk | xargs rm # is dangerous due to The separator problem
<https://en.wikipedia.org/wiki/Xargs#The_separator_problem>.

~~~
rcthompson
ls | grep junk | xargs trash-put

<https://github.com/andreafrancia/trash-cli/#readme>

------
bitops
OP here - thanks all for the comments - so yes, it looks like I don't know
xargs as well as I thought I did. I'll update the post to be more accurate.

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.

