How to Count Files in Directory on Linux

If you need to count how many files are in a directory on Linux, then you can use a combination of the ls command to list all the files, and the wc command to count how many lines are printed:

Option 1 – Using wc

ls | wc -l
Code language: Bash (bash)

You can specify a directory as follows:

ls <directory> | wc -l
Code language: Bash (bash)

Option 2 – Using find

You can count files recursively by using the find command:

find <directory> -type f | wc -l
Code language: HTML, XML (xml)

Option 3 – Using tree

Using the tree command will print out the amount of directories and files found, but if used in a large directory, then this will print a lot of output to the screen, so be careful with this one.

Code language: Bash (bash)

This command will not count hidden files, so add the -a attribute to include them:

tree -a <directory>
Code language: Bash (bash)

Notify of
Inline Feedbacks
View all comments