Linux: count number of files in a directory

by CarcaBot on July 23, 2009 · 0 comments

The following command will print the number of files in the current directory:

ls | wc -l

The ‘ls’ command prints the directory listing. The output of ls is piped into the wc command by the inclusion of the ‘|’. Finally, the ‘wc’ command performs a word count on the directory listing it was given, and because ls outputs each filename as a word, you get the number of files in the directory.

Next post: