How to Count number of files within a directory in Linux
Linux system administrators monitor the system all the time to keep an eye on memory usage, number of files created by the users.
There are various methods to count the number of files in a directory:
- Count number of Files Using ls with wc Commands
- Count number of Files Using find with wc Commands
- Count number of Files Using tree Command
- Count number of Files Using GUI (Linux Mint 21)
1: Count number of Files in Linux Using ls with wc Commands
The “wc” command can count total words, lines, character, and byte. Let’s use it with the “ls” command that will count total files of a directory. Following syntax will be followed:
For example, to count the number of files in Pictures directory the following command will be used:
Similarly, to check the files in etc directory use:
2: Count number of Files in Linux Using find with wc Commands
Another way of counting the files in a particular directory is using the “find” and “wc” command:
For example, to count number of files in Pictures directory, use:
The “f” flag is used to target the files only.
To find out the number of files count in /etc directory use:
The error message can be removed or can be redirected to /dev/null directory using 2> redirection operator. So, the above command would be:
Note: It is important to note the find command will be counting the file recursively, which means it will count all the files in the subdirectories as well. So the output can vary.
3: Count number of Files in Linux Using tree Command
The third command that can count the number of files in a directory is tree command. It is not available by default; it needs to be installed:
To get a count of number of files in a directory (Pictures), use:
Number of files can be seen at the end of the output.
4: Count number of Files in Linux Using GUI (Linux Mint 21)
To find the number of files in a directory through GUI simply right click on the directory, then from the context menu open “Properties”.
There you can see the total items.
Conclusion
Counting directories files in Linux is an easy task that can be done using CLI (command line interface). Using article steps, we can count the number of files in any directory on your Linux system. Three commands which include ls, find and wc are mainly used for counting the number of files inside a directory in the Linux system.