Xargs

From LSDevLinux
Jump to: navigation, search

xargs can be used to put the output of one program onto the command line of another.

For example:

find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

Will find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing spaces or newlines are correctly handled.

(Example taken from xargs man page)

xargs will interpret all whitespace as argument separators, this can be a problem, especially if there are filenames in the input. The -0 option will separate on null characters rather than whitespace. Nulls can be output from find using the -print0 option as seen above. Otherwise, line endings can be translated to nulls using the following:

tr '\n' '\0'