Linux Applications

How to Count Number of Files within a Directory in Linux

In Linux, there are a number of methods to check and count total files in a directory. Counting files can be helpful specially automating the tasks while bash scripting and monitoring the system. In this article, we will go over different methods to count the number of files within a directory in Linux.

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:

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:

$ ls /<directory-name> | wc -l

For example, to count the number of files in Pictures directory the following command will be used:

$ ls ./Pictures | wc -l

Similarly, to check the files in etc directory use:

$ ls /etc | wc -l

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:

$ find <directory-name> -type f | wc -l

For example, to count number of files in Pictures directory, use:

$ find ./Pictures -type f | wc -l

The “f” flag is used to target the files only.

To find out the number of files count in /etc directory use:

$ find /etc -type f | wc -l

The error message can be removed or can be redirected to /dev/null directory using 2> redirection operator. So, the above command would be:

$ find /etc -type f 2> /dev/null | wc -l

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:

$ sudo apt install tree

To get a count of number of files in a directory (Pictures), use:

$ tree Pictures

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.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.