12

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?

Nakilon
  • 144
Tim
  • 101,790

4 Answers4

6

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

Lencell
  • 109
  • re. the last paragraph, without -d '\n', one line with two words results in two args to the command with -L1. It's just that with -L1 it doesn't join args from multiple input lines, it still splits them as usual – ilkkachu Jun 06 '18 at 21:59
  • I agree, tho the emphasis was more that with a foo bar output from locate, with -L you get rm foo bar, and with -n you would rather get rm foo rm bar. Which I'll agree have no impact here but might have with a command other than rm. – Lencell Jun 06 '18 at 22:08
  • Thanks. What does "Trailing blanks cause an input line to be logically continued on the next input line" mean? https://unix.stackexchange.com/questions/448333/what-does-trailing-blanks-cause-an-input-line-to-be-logically-continued-on-the – Tim Jun 07 '18 at 05:07
  • @Lencell, it matters a bunch, if you have filenames with spaces. Well, unless locate produces output with quoting compatible with xargs, that is. Two arguments foo and bar is different from one argument foo bar. – ilkkachu Jun 07 '18 at 08:07
3

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.

DopeGhoti
  • 76,081
3

-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

Related: https://stackoverflow.com/questions/6527004/why-does-xargs-l-yield-the-right-format-while-xargs-n-doesnt

Ciro Santilli OurBigBook.com
  • 18,092
  • 4
  • 117
  • 102
2

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.

schily
  • 19,173