This question was prompted by questions about ls
' -1
option and the recurring tendency of people to ask question and answers that includes processing the output of ls
.
This reuse of the output ls
seems understandable, e.g.: if you know how to sort a list of files with ls
you might want to use the output in that way as input for something else.
If those Q&A don't include a reference to the file name list produced consisting of nicely behaving file names (no special characters like spaces and newlines), they are often commented upon by someone pointing out the danger of the command sequence not working when there are files with newlines, spaces etc.
find
, sort
and other utilities solve the problem of communicating "difficult" file names to e.g. xargs
by using an option to separate the file names with the NUL character/byte which is not a valid character in file name (the only one in addition to /
?) on Unix/Linux filesystems.
I looked to the man page for ls
and the output for ls --help
(which has more options listed) and could not find that ls
(from coreutils
) has an option to specify NUL separated output. It does have a -1
option which can be interpreted as "output file names separated by newline")
Q: Is there are technical or philosophical reason why ls
does not have a --zero
or -0
option that would "output file names separated by NUL"?
If you do something that only outputs the file names (and not use e.g. -l
) that could make sense:
ls -rt -0 | xargs -r0 …
I could be missing something why this would not work, or is there an alternative for this example that I overlooked and that is not much more complicated and/or obscure.
Addendum:
Doing ls -lrt -0
probably does not make much sense, but in the same way that find . -ls -print0
does not, so that is not a reason to not provide a -0
/-z
/--zero
option.
Best Answer
UPDATE (2014-02-02)
Thanks to our very own @Anthon's determination in following the lack of this feature up, we have a slightly more formal reason as to why this feature is lacking, which reiterates what I explained earlier:
My original answer
This is a bit of my personal opinion but I believe it to be a design decision in leaving that switch out of
ls
. If you notice thefind
command does have this switch:By leaving that switch out, the designers were implying that you should not be using
ls
output for anything other than human consumption. For downstream processing by other tools, you should be usingfind
instead.Ways to use find
If you're just looking for the alternative methods you can find them here, titled: Doing it correctly: A quick summary. From that link these are likely the 3 more common patterns:
(nonstandard common extensions -print0 and -0. Works on GNU, *BSDs, busybox)
Further evidence?
I found this blog post from Joey Hess' blog titled: "ls: the missing options". One of the interesting comments in this post:
Further searching I found this in the commit logs from one of the additional switches that Joey's blog post mentions, "new output format -j", so it would seem that the blog post was poking fun at the notion of ever adding a
-z
switch tols
.References