Xargs by example

From Sidvind
Revision as of 17:36, 20 May 2008 by EXt (Talk | contribs)

Jump to: navigation, search

Examples

Using a placeholder

Code: Lists all directories

find . -maxdepth 1 -type d -print | xargs echo Directories:

This will print all directories in the current folder. The command (echo) specified will recived the input from find and be executed once. That's why this code will output all directories on one line.

Code: Lists all directories using -I

find . -maxdepth 1 -type d -print | xargs -I {} echo Directory: {}

This time we added -I {} to xargs. This is the replacement argument. The command specifed to xargs will now be executed once for each line of output from find and replace {} with that line.

So this time we get each directory printed on a separate line.

Note: If xargs -i fails you might need a newer version of xargs. The Gentoo package findutils contains xargs.

Keep this in mind when using xargs. When creating more complex commands you often need to use the -I argument. Like if you want to run multiple commands and/or pipe to another command.

Multiple lines as one argument

Code:

ls | xargs -L 4 echo

Using the -L argument we can concatenate n lines into one (separated with spaces of course). In this case it will output four files/directories on each line.

Custom delimiters

Code: Using comma as delimiter

echo "foo,bar,baz" | xargs -d, -L 1 echo

The -d argument is used to use a custom delimiter, c-style escaping is supported (\n is newline for instance). In this case it will output foo, bar and baz on a separate line.

Read from file instead of stdin

Code:

xargs -a foo -d, -L 1 echo

The -a argument is used to read from a file instead of stdin. Otherwise this example is the same as the previous.

Showing command to be executed

Code:

ls | xargs -t -L 4 echo

Before running the command -t will cause xargs to print the command to run to stderr. In this case it will output "echo fred barney wilma betty" before running that same line.

Handling paths with whitespace etc

Code:

find . -print0 | xargs -0 echo

Each argument passed from find to xargs is separated with a null-terminator instead of space. It's hard to present a case where it is required as the above example would work anyway. But if you get problems with paths which may contain whitespace, backspaces or other special characters use null-terminated arguments instead.

Snippets

Code: Cleans current directory from all subversion directories recursively.

find . -type d -name ".svn" -print | xargs rm -rf

The above command will execute rm on each file found by 'find'. The above construct can be used to execute a command on multiple files. This is similar to the -exec argument find has but doesn't suffer from the "Too Many Arguments" problem. And xargs is easier to read than -exec in most cases.