Counting files in a directory

When browsing your directories command line on your linux box you might want to get a count of the files inside a particular directory. This turns out to be a very easy task and the only things you need to know are the | operator and two basic commands, called ls and wc.

Since you are browsing your directories command line I take it you already know ls (for listing directory contents) but you might not yet know wc!

So first you need to issue an ls -l command which will print on standard output (your terminal) a list of the files in your directory one file at each line. Then we need to pass that result as input to the wc command (which prints information about the lines in a file, or the words in a file etc.). So, we can do that in one line by writing:
ls -l | wc -l

Leave a Reply