The Joys of xargs
xargs and find
xargs is an incredibly useful command: it takes in input and executes your chosen command on it: xargs command. Deceptively simple in concept; extremely powerful in execution. It's most commonly used to execute one command on the output from another command (command1 | xargs command2), and most often the output-generating command is find (xargs is more flexible than the built-in -exec option). Here we'll look at xargs with find, and then at some other possibilities.
A couple of options which are useful for testing: -t echoes the command before executing it, and -p echoes it and asks for confirmation.
xargs and find
find and xargs do go very well together: find to locate what you're looking for, and xargs to run the same command on each of the things found.
Traditionally, an advantage to xargs was its ability to handle long command lines before failing, unlike some other commands. This command:
rm `find tmp -maxdepth 1 -name '*.mp3'`is intended to remove all tmp/*.mp3 files (and ignore any subdirectories), but can fail with an "Argument list too long" message. This exact equivalent:
find tmp -maxdepth 1 -name '*.mp3' -maxdepth 1 | xargs rmdoes exactly the same thing but will avoid the problem by batching arguments up. More modern kernels (since 2.6.23) shouldn't have this issue, but it's wise to make your scripts as portable as possible; and the xargs version is also easier on the eye.
You can also manually batch arguments if needed, using the -n option.
find tmp -maxdepth 1 -name '*.mp3' -maxdepth 1 | xargs -n1 rmwill pass one argument at a time to rm. This is also useful if you're using the -p option as you can confirm one file at a time rather than all at once.
Filenames containing whitespace can also cause problems; xargs and find can deal with this, using GNU extensions to both to break on the null character rather than on whitespace:
find tmp -maxdepth 1 -name *.mp3 -print0 | xargs -0 rmYou must use these options either on both find and xargs or on neither, or you'll get odd results.
Another common use of xargs with find is to combine it with grep. For example,
find . -name '*.pl' | xargs grep -L '^use strict'will search all the *.pl files in the current directory and subdirectories, and print the names of any that don't have a line starting with 'use strict'. Enforce good practice in your scripting!
Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.
- 1Linux Top 3: Alienware, KDE and Ubuntu 13.04
- 2Linux Top 3: Linux Mint Olivia, Fedora 19's Cat and Ubuntu's Mission Accomplished Moment
- 3GNOME 3.8 Debuts New Open Source Linux Desktop
- 4Linux Top 3: Linux 3.10 Goes Long, Linux 3.11 Advances as LXDE Merges
- 5Linux Top 3: Ubuntu Kaylin, Debian Wheezy and Linux Mint