From Search for and remove files safely
locate -i nohup.out | xargs -d '\n' -L1 -p rm
Each line in the output of locate is treated as a argument by xargs, so are -L1 and -n 1 the same?
From Search for and remove files safely
locate -i nohup.out | xargs -d '\n' -L1 -p rm
Each line in the output of locate is treated as a argument by xargs, so are -L1 and -n 1 the same?
From the manual:
-L max-lines
Use at most max-lines nonblank input lines per command line. Trailing blanks cause an input line to be logically continued on the next input line. Implies -x.-n max-args
Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.-d delim
Input items are terminated by the specified character. [...]
Based on this and my understanding, in your case -L1 and -n1 are made equivalent both by the argument 1 passed and the delimiter changed from blank space to \n (newline) by the argument -d
For example, without the -d argument if you were to have a blank space in your locate output, then this line would be splitted into two arguments and hence 2 different use of rm with -n1, while it would still be treated as one argument and only one command with -L1
It appears that the difference, based on reading the manual, is that -L filters for nonblank lines, while -n does not. Presuming that locate will never output a line containing only whitespace, they should be functionally identical in this use-case.
-n splits on any whitespace, -L splits on newlines
They therefore produce different outcomes:
printf '1 2\n3 4\n' | xargs -L1 echo
splits by line and therefore is equivalent to:
echo 1 2
echo 3 4
which outputs:
1 2
3 4
However:
printf '1 2\n3 4\n' | xargs -n1 echo
splits on any whitespace, and is therefore equivalent to:
echo 1
echo 2
echo 3
echo 4
and produces instead:
1
2
3
4
The option -L is an XSI extension and is not required to be present on embedded systems.
The -n option is part of the basic standard and always works.
See the standard as a reference: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/xargs.html
Note that some implementation may also switch the behavior for lines that end in spaces while others may concatenate a line ending in space regardless of whether the -L option has been specified.
-d '\n', one line with two words results in two args to the command with-L1. It's just that with-L1it doesn't join args from multiple input lines, it still splits them as usual – ilkkachu Jun 06 '18 at 21:59foo baroutput from locate, with-Lyou getrm foo bar, and with-nyou would rather getrm foorm bar. Which I'll agree have no impact here but might have with a command other than rm. – Lencell Jun 06 '18 at 22:08locateproduces output with quoting compatible withxargs, that is. Two argumentsfooandbaris different from one argumentfoo bar. – ilkkachu Jun 07 '18 at 08:07