In this tutorial, we will learn about How to Recursively Count the Number of Files within a Directory in Linux? Sometimes we need to find the actual number of files available under a directory. But its directory contains multiple subdirectories. Then it is hard to manually count the number of files within a directory in a Linux system using the command line.
find DIR_NAME -type f | wc -l
- find – Is a Linux/Unix command
- DIR_NAME – A directory path to search for. Use dot (.) to start the search from the current directory
- -type f – Search for files only (do not include directories)
- Pipe (|) – Pipe sends the output of one command as input to another command
- wc -l – Count number of lines in result
Count Files within the Current Directory
Use the following command to count the number of available files under the current directory. Here dot (.) denotes to the current directory.
find . -type f | wc -l
Count Files in a Specific Directory
To count files under any other directory use the following command. Here the command will find all files under the /backup directory and print the total count on the screen.
find /backup -type f | wc -l