NAS Storage Blog

NAS storage news, reviews, tips and tricks

How to count the number of files in a directory in Linux

It is easy to count the number of files and subdirectories in some directory in a Linux OS. With the use of the command ls and wc you can create the command that will do this.

Count number of files and subdirectories:
ls -l | wc -l

Count number of subdirectories only:
ls -l | grep ^d | wc -l

Count number of files only (inverted grep command):
ls -l | grep -v ^d | wc -l

I just love the way that the output from one command can be send as input to another command also known as piping. This is so powerful.

Leave a Reply