How to count files in a directory and it's subdirectories on Linux
If you want to count all files in a directory, including sub-directories from the command line, you can use the find command.
The find command is a powerful tool capable of searching for files in a file systems directory tree. Here we will pipe the output to wc to count the files it finds instead of printing them to the terminal.
To count files in a directory and it's sub-directories:
$ find /path/to/directory -type f | wc -l
In this example we will count all the files in /home/savona/Desktop/TEMP/Putorius directory.
$ find ~/Desktop/TEMP/Putorius/ -type f | wc -l 9112
To count all the files in your current working directory:
$ find . -type f | wc -l
Find would normally print out all the files it found to stdout. Instead we pipe the output of the find command into wc and use the -l (--lines) switch to count those lines.
The find command makes tasks like this simple. It is a powerful and well documented tool for search files from the command line.
Resources and Links
This site uses Akismet to reduce spam. Learn how your comment data is processed.