How To Count The Number Of Files In Linux

The easiest way to count the number of files in Linux is using the find and wc commands. This how to count the number of files in Linux explains various options. 

Index

How To Count The Number Of Files In Linux

The most simplest way is using a combination of the ls and wc commands:

find -maxdepth 1 -type f | wc -l

This command provides you a simple number as an output. As you can see in the below picture, this is the of files, not including directories.

How To Count The Number Of Files In Linux

How To Count The Number Of Files In Linux

Now if you like to also include the directory, in this example called directory-1, than the command to be used is even simpler:

ls -A1 | wc -l 

This gives you as the output the number for both, the number of files and directories. In this example 4.

Other Powerful Options To Count The Number Of Files In Linux

Linux consists of very powerful commands. Because of this, the above examples highlight only two options. Lets explore which other options we have since these maybe are more useful for your use case.

ls -lA | grep "^\-" | wc -l

This command outputs the same result as find -maxdepth 1 -type f | wc -l, but it is leveraging on the ls instead of the find command. Because there is another option for find, lets check this one as well: Leaving out the maxdepth part is possible too by simply executing “find -type f | wc -l”

Further Information

ls – helps to list directory contents. There are multiple other options that this powerful command allows. Check out the ls man page!

wc – prints newline, word, and byte counts. This command is another very powerful one in Linux. Check out the wc man page for other cool options.