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
You can specify a directory as follows:
ls <directory> | wc -l
Option 2 – Using find
You can count files recursively by using the find
command:
find <directory> -type f | wc -l
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.
tree
This command will not count hidden files, so add the -a
attribute to include them:
tree -a <directory>