--- origxargs.txt 2013-08-20 14:37:37.242700950 -0400 +++ xargs.txt 2013-08-20 14:34:13.366606370 -0400 @@ -16,30 +16,42 @@ [--help] [command [initial-arguments]] DESCRIPTION - This manual page documents the GNU version of xargs. xargs reads items - from the standard input, delimited by blanks (which can be protected - with double or single quotes or a backslash) or newlines, and executes - the command (default is /bin/echo) one or more times with any initial- - arguments followed by items read from standard input. Blank lines on - the standard input are ignored. - - The command line for command is built up until it reaches a system-de- - fined limit (unless the -n and -L options are used). The specified - command will be invoked as many times as necessary to use up the list - of input items. In general, there will be many fewer invocations of - command than there were items in the input. This will normally have - significant performance benefits. Some commands can usefully be exe- + This manual page documents the GNU version of xargs. xargs -d\\n reads + items from the standard input, delimited by newlines, with no addition- + al interpretation, and executes the command (default is /bin/echo) one + or more times with any initial-arguments followed by items read from + standard input. Every newline in the input yields an argument to com- + mand, and an initial newline or pairs of consecutive newlines yield + empty arguments. The final newline may be omitted. xargs -d\\n cor- + rectly processes filenames, easily created using GUI applications, that + contain blanks, quotes, or backslashes. + + The command line for command is built up until it reaches a system-de- + fined limit (unless the -n and -L options are used). The specified + command will be invoked as many times as necessary to use up the list + of input items. In general, there will be many fewer invocations of + command than there were items in the input. This will normally have + significant performance benefits. Some commands can usefully be exe- cuted in parallel too; see the -P option. - Because Unix filenames can contain blanks and newlines, this default - behaviour is often problematic; filenames containing blanks and/or new- - lines are incorrectly processed by xargs. In these situations it is - better to use the -0 option, which prevents such problems. When using - this option you will need to ensure that the program which produces the - input for xargs also uses a null character as a separator. If that - program is GNU find for example, the -print0 option does this for you. + Without the -d\\n option, xargs reads items from the standard input, + delimited by runs of consecutive newlines, tabs, and blanks, interpret- + ing single and double quotes and backslashes in a reasonable way. + Leading and trailing whitespace is ignored. A newline may only be + quoted by a backslash. An empty argument is denoted by an isolated + pair of quotes. When standard input comes from a command which does + not quote its output, default xargs does not do what you want. + + Because Unix filenames can contain newlines as well as the characters + handled by -d\\n, but cannot contain null characters, the only unam- + biguous list of unquoted file names uses null delimiters. Commands + like find -print0, perl -0, sort -z, and grep -lZ write null-delimited + lists, and xargs -0 accepts them. This is the most robust way to deal + with arbitrary file names, although it still fails if the directory + structure changes after find writes its output and before command opens + its last file. - If any invocation of the command exits with a status of 255, xargs will + If any invocation of command exits with a status of 255, xargs will stop immediately without reading any further input. An error message is issued on stderr when this happens. @@ -49,10 +61,9 @@ whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input - items might contain white space, quote marks, or backslashes. - The GNU find -print0 option produces input suitable for this - mode. - + items might contain newlines, blanks, quote marks, or backslash- + es. Commands like find -print0, perl -0, sort -z, and grep -lZ + produce input suitable for this mode. -a file, --arg-file=file Read items from file instead of standard input. If you use this @@ -176,24 +187,35 @@ find /tmp -name core -type f -print | xargs /bin/rm -f Find files named core in or below the directory /tmp and delete them. - Note that this will work incorrectly if there are any filenames con- - taining newlines or spaces. + Note that this will work incorrectly if there are any file or directory + names containing spaces, tabs, quotes, backslashes, or newlines. find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names con- - taining spaces or newlines are correctly handled. + taining spaces, tabs, quotes, backslashes, or newlines are correctly + handled. find /tmp -depth -name core -type f -delete - Find files named core in or below the directory /tmp and delete them, + Find files named core in or below the directory /tmp and delete them, but more efficiently than in the previous example (because we avoid the - need to use fork(2) and exec(2) to launch rm and we don't need the ex- + need to use fork(2) and exec(2) to launch rm and we don't need the ex- tra xargs process). + find /tmp -name core -type f -execdir /bin/rm -f '{}' + + + Find files named core in or below the directory /tmp and always delete + the intended file, even when file or directory names contain spaces, + tabs, quotes, backslashes, or newlines, when the directory structure of + /tmp has changed after the file was found and before the rm command is + executed, and even when the directory contains an executable file named + rm. + + cut -d: -f1 < /etc/passwd | sort | xargs echo Generates a compact listing of all the users on the system.