Linux: count number of files in a directory

Posted: October 31st, 2007 | Author: steve | Filed under: linux |

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.


2 Comments on “Linux: count number of files in a directory”

  1. #1 Johnny said at 9:16 am on January 18th, 2008:

    Thank you man!
    your solution works great.

  2. #2 Chris said at 3:33 am on January 6th, 2009:

    Unfortunately this wont work on directories with a lot of files in them; you’ll get the “Argument list too long” error…


Leave a Reply