Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `ls | cat` != `ls`?

Tags:

bash

shell

unix

Why is there a difference in output between these two commands:

ls | cat

ls

The first one seems to separate filenames with a newline.

This also applies to commands suc has ls > outfile, and similar things.

I'm on Mac OSX, if that makes any difference.

like image 412
conradkleinespel Avatar asked May 02 '14 00:05

conradkleinespel


People also ask

Why my ls command is not working?

Why does LS not work in CMD? - Quora. Because ls is a bash command. CMD and bash are two entirely different command-line shells. The equivalent command in CMD for ls is DIR.

What does the '- L option in ls command mean?

ls -l. The -l option signifies the long list format. This shows a lot more information presented to the user than the standard command. You will see the file permissions, the number of links, owner name, owner group, file size, time of last modification, and the file or directory name.

What is the result of ls * and why?

If ls is being passed an argument called * , it will look for a file or directory called * in the current directory and list it just like any other. ls doesn't treat the * character in any other way than any other one.


1 Answers

The ls command checks the file type of its standard output. If it detects a “tty”, it emits multicolumn output. If it detects any other file type (like a disk file or a pipe), it emits single column output.

Tty is short for teletype. In the old days, you would use an actual teletype to interact with a Unix system. So if standard output was a teletype, ls would produce output optimized for humans. Otherwise, it would produce output optimized for programs.

With the advent of other ways to run an interactive shell, like telnet sessions and window systems, the Unix authors created the pseudo-teletype, or “pty”, a software-only “device” which pretends to be a teletype. One program (like the telnet server, the ssh server, or the terminal window) can use a pty to make another program (the shell) think it is talking to a teletype.

You can use the -C flag to force ls to emit multicolumn output. You can use the -1 (digit one) flag to force single column output.

like image 160
rob mayoff Avatar answered Sep 28 '22 04:09

rob mayoff