If you don’t know what xargs is, here’s the definition of xargs from Wikipedia:
xargs is a command of the Unix and most Unix-like operating systems which eases passing command output to another command as command line arguments.
It splits its often piped input at whitespaces (or the null character) and calls the command given as an argument with each element of the split input as parameter. If the parameter list is too long, it calls the program as often as necessary. It often covers the same functionality as the backquote feature of many shells, but is more flexible and often also safer, especially if there are blanks or special characters in the input.
This is often used in conjunction with the Unix commands find, locate and grep.
So, what can you do with xargs?
Well, let’s say you want to put up a torrent on a tracker, but you want to include a file with the MD5 checksums of every file. We’ll assume that the files you want to put on bittorrent are in /home/me/mytorrent/
To generate an MD5 sum of every file in the directory and dump it to a file, you can do the following:
find /home/me/mytorrent/ -type f|xargs md5sum >> /home/me/mytorrent/md5sums.txt
Now you’ve got a list of the MD5 sums for every file in the torrent you want to publish.
You can also use xargs to call grep to look inside files. For instance, if you’re editing a web page and you want to find all instances of a PHP class, you can execute the following:
find /home/me/mywebpage -name '*.php'|xargs grep -Hn adminClass
This will return a list of each file ending in php, and then for each item in the list, grep will check for the string adminClass. The -H flag will make sure that grep prints the filename in each match, and the -n flag will make grep print the line number as well.
xargs is an invaluable tool in command-line work because it prevents you from having to write a for or while loop in a shell script. xargs will run the command you specify on every line of output. Granted, find has an -exec capability, but xargs has its utility. Being slightly paranoid, you might like to check the output of a command before you pipe it to xargs or run an -exec in find. For instance, if you were removing everything in a cache directory, it would probably be better to pipe the find output to more first, and then after you have verified the output, you can pipe it to xargs rm -f -r
For usenet users, you can also use xargs to PAR check all of your files.
ls *.par2|xargs par -r
Windows users take heart, there is an xargs for windows in the Findutils package for Windows .
Have any other xargs tricks? Leave yours in the comments.