Preparing output for further processing with Xargs

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Nesting without Nests

If you want to nest your searches, with Xargs you create several redirections. However, you can't simply append the directories to search at the end. Instead, use the -I option to define a text string that you want to be replaced by the filename. In the example, I chose the string HERE (Listing 10).

Listing 10

Nesting with Xargs

01 $ find . -name Podcasts -type d | xargs -I HERE find HERE -name '*.ogg' | xargs file
02 ./long/Podcasts/dh-20130204-ver-045.ogg: Ogg data, Vorbis audio, stereo, 44100 Hz, ~91840 bps
03 ./long/Podcasts/dh-20130107-ver-044.ogg: Ogg data, Vorbis audio, stereo, 44100 Hz, ~91840 bps
04 ./short/Podcasts/dh-20121015-sh-018.ogg:    Ogg data, Vorbis audio, stereo, 44100 Hz, ~91840 bps

This approach is useful when you want to access the mv or cp commands that like to have two parameters, with the last one being the target directory.

Linux – prior to version 2.6.23 (October, 2007) – and some other UNIX systems have limits to the number of command-line parameters that can be passed to another program. With backticks or the $() construct, and if the inside command outputs more than 1024 words, the shell breaks down. Here again, Xargs comes to the rescue. It knows of this limit and separates overlarge parameter lists into segments acceptable by the operating system.

Separate Treatment

Many command-line programs process only one file per call, among them GnuPG. Xargs provides the -n option to indicate how many parameters to accept per call (Listing 11). If you also add the -P with a number value, the software creates a corresponding number of instances for passing the parameter.

Listing 11

Limiting the Number of Parameters

01 $ find . -name '*.gpg' | xargs -n 1 gpg
02 [...]

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content