9/24/2014

different ls output to terminal and pipe

Try these commands:

ls -1 | wc -l
ls | wc -l
ls | cat

We can use the first two commands to count the number of files. It is easy to understand the first one works. But the second command does not seem to work as expected at the first glance. However, you will find it really works the same way as the first one. The reason can be seen from the last command -- ls outputs the results to pipe in the same way as "ls -1" to terminal.

http://unix.stackexchange.com/questions/157285/why-does-ls-wc-l-show-the-correct-number-of-files-in-current-directory

http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c

No comments:

Post a Comment