Posted 2005-06-21T20:10:00+01:00 in unix
GNU xargs is a nifty utility that moves text from standard input to the command argument space. A few weeks back I discovered you can do loop stuff with it more easily than regular shell while’s or for’s:
cat files.txt | while read F
do echo "This line is named $F"
done
versus
xargs -n 1 echo "This line is named"
Of course this is a useless example, but it demonstrates two interesting features.
The -n argument tells xargs how many lines from standard input should be bundled together for one execution of the following statement. If you were to change -n to 2, you would see two filenames on each line.
I often find myself piping file listings into a file, and then doing various stuff with that listing, e.g. grepping for code and so on.
xargs grep 'SomeSillyClass' < files.txt