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
234
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.
Conclusion
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
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)