Why does xargs -L yield the right format, while xargs -n doesn't?
-L
splits by lines; echo
doesn't separate its output by lines but by spaces, so a single ls -l
is run and that formats all the columns as a group.
-n
splits by parameters; in the absence of -L
or -0
, the separator is whitespace (possibly modified by quoting), so each filename gets its own ls -l
run and there is no way for the independent runs to coordinate column widths.
The POSIX standard mandates:
-L
numberThe utility shall be executed for each non-empty
number
lines of arguments from standard input. The last invocation of utility shall be with fewer lines of arguments if fewer than number remain. A line is considered to end with the first unless the last character of the line is a<blank>
; a trailing<blank>
signals continuation to the next non-empty line, inclusive.
-n
numberInvoke utility using as many standard input arguments as possible, up to number (a positive decimal integer) arguments maximum.
(Emphasis added.) Since echo *
produces a single line, xargs -L 1
just feeds all of the filenames to ls
at once, and only then can ls
nicely align the columns.
(In other words, your first command is equivalent ls -l index.html*
, except that it doesn't handle filenames containing blanks correctly.)