Introduction
Is a very useful program to take a list and run commands against that list. xargs will take a list of arguments, loop through them and run a command against 1 or more arguments, one at a time.
If you like xargs you might want to check out GNU Parallel.
Basic Example
Here is a really straightforward non-destructive example of using xargs to calculate a MD5 hash on every file in the current directory,
ls | xargs -t md5sum
This is how it works,
- -t will show you what xargs is about to execute before it executes it.
- xargs will by default take the output of ls one line at a time and append it to the end of the command
Thanks to the -t the output will be shown on screen,
md5sum planetary.doc ab5970d50d67bcafe5c554387f76534e = planetary.doc md5sum Superman.jpg cdefa50d737dfcf8dc57886ea1a758c4 = Superman.jpg
Substitution to Rename Files
Now let's get more advanced and use -I to allow substitution and explicitly set the location of what xargs receives. First we'll create a some temporary files,
mkdir temp cd temp touch files1 file2 file3 # Creates 3 empty files
Now using xargs we will add the txt extension to each file,
ls | xargs -t -I{} mv {} {}.txt mv file1 file1.txt mv file2 file2.txt mv file3 file3.txt
The -I{} specifies that the arguments by ls will be placed in the location of the {} called the replacement string. In fact you can use whatever variable name you want instead of {}. For example, the following will also work,
ls | xargs -t -IvarX md5 varX md5 file1.txt MD5 (file1.txt) = d41d8cd98f00b204e9800998ecf8427e md5 file2.txt MD5 (file2.txt) = d41d8cd98f00b204e9800998ecf8427e md5 file3.txt MD5 (file3.txt) = d41d8cd98f00b204e9800998ecf8427e
One item I don't understand yet is why {} forces arguments to be iterated through one at a time. Also, how would we allow more than one argument? -n2 will not work.
Debugging xargs with echo
The echo command is useful to test and see what xargs will be looping through,
ls | xargs -I{} echo "mv {} {}.txt" mv file1 file1.txt mv file2 file2.txt mv file3 file3.txt
Notice that using echo I omit the -t but you will want to put the -t back when you are actually executing your command.
Dealing with Special Characters
When using xargs it will not work with special characters like apostrophe in file names. To get around this limitation use the find command's -print0 option in combination with -0,
find . -print0 | xargs -0 -I{} echo {}
Note using xarg's -0 "The GNU find -print0 option produces input suitable for this mode." it probably is because it is meant to be used together.
Useful Applications of xargs
Search - ...