In article Count Files in Directory in Linux we showed a simple method to count files in a single directory with ls | wc -l
. In order to count number of files in a directory and its subdirectories, we can use more sophisticated find
command.
Go to your folder and type:
$ find . -type f
./.gitignore
./booking.php
./file1.php
./README.md
./file3.php
./subfolder/.gitignore
./subfolder/README.md
This outputs all the files recursively in that folder.
.
stands for the “starting point” of the search-type f
tells the find to find only files (not directories)However, we wanted to know the number of files, not the list of files. For this we use the wc -l
to calculate number of lines of the input.
$ find . -type f | wc -l
7
This outputs a 7 which is correct.