Thursday, January 19, 2012

xargs with multiple commands as arguments

The -c option of sh command causes the commands to be read from the string operand instead of from the standard input. Keep in mind that this option only accepts a single string as its argument, hence multi-word strings must be quoted.

# find /usr/home -type f -print0 | xargs -0 -I {} sh -c 'cmd1 "{}" ; cmd2 "{}" ; cmd3 "{}"'

Example:
# find /usr/home -type f -print0 | xargs -0 -I {} sh -c 'ls -ld "{}" ; file "{}"'

Note: the double quotes are required if the filename contains a space.

No comments: