I think I may be overlooking a relatively fundamental point regarding shell. Output from the ls command by default separates output with newlines, but the shell displays the output on a single line.
Can anyone explain this to me? I had always presumed that the output was simply separated by spaces, but now that I see the output separated by newlines, I would expect the output to be displaying on separate lines.
Example:
cpoweradm@debian:~/lpi103-4$ ls text*
text1 text2 text3
od shows that the output is separated by newlines:
cpoweradm@debian:~/lpi103-4$ ls text* | od -c
0000000 t e x t 1 \n t e x t 2 \n t e x t
0000020 3 \n
0000022
If newlines are present, then why doesn't the output display as:
text1
text2
text3
Best Answer
When you pipe the output,
ls
acts differently.This fact is hidden away in the info documentation:
To prove it, try running
and then
This means that if you want the output to be guaranteed to be one file per line, regardless of whether it is being piped or redirected, you have to run
(
-1
is the number one)Or, you can force
ls | less
to output in columns by running(
-C
is a capital C)