How to Count files in Directory of Linux?

In Linux, directories are the folders used in the Windows operating system. In Linux, different directories are available by default that contains the system and user files. Linux OS supports a list of commands to handle the directories and perform various operations on it. Apart from using commands, a GUI method can also be followed to count the number of files in the directory. This post will illustrate the possible methods to find the number of files in a directory. The content supported by this guide is as follows:

Let’s start today’s guide!

How to Count Files in the Directory of Linux Using Terminal?

The command line terminal of any Linux distribution is strong enough to perform any operation on the system. In this section, the commands are practiced to count the files in a directory.

Using the ls Command With wc

The ls command is used to list down the contents of any directory, but if it is piped with the wc command line utility, it will display the count of the files in the directory.

The general syntax of using the ls command piping with the wc command to count files in the directory is:

$ ls [path] | wc -l

The output of the ls command is provided as an input to the wc command, and then wc provides the total count of the files.

For example, we will count the files of the “/home/itslinuxfoss/Downloads” directory using the command:

$ ls /home/itslinuxfoss/Downloads| wc -l

The file count of the “/home/itslinuxfoss/Downloads” directory has been displayed.

Using the find Command With wc

The other method to count the files of the specified directory is by using the find command utility piping with the wc command. The general syntax of using the find command to count the files of the directory is:

$ find [directory] -type f | wc -l

In the above syntax, the find command utility is used to find out the specified directory, its “type” option is used to sort out all the files because of using the “f” option. And then, the wc command utility will display the count of the files.

For example, the count of the files in the “/opt” directory can be displayed using the command:

$ sudo find /opt -type f | wc -l

Using the tree Command

The tree command utility can also be used to count the files of the directory. To install the tree command utility, run the command:

$ sudo apt install tree -y

After installing the tree command utility, run the command with the directory’s path to count the files. For example, we will count the files of the “Downloads” directory using the tree command:

$ tree Downloads

It is observed from the output that there is only one file inside the “Downloads” directory.

Using the rsync Command

The rsync command is mostly related to the purpose of the backup/synchronization of files. However, it can also count the number of files in the directory. It can be installed on Linux using the command:

$ sudo apt install rsync -y

After installing the “rsync” command utility, it can be used to find the count of files in any directory. For example, we will count the files of the /etc using the rsync command:

$ sudo rsync -stat --dry-run -a /home/itslinux/ | wc -l

That’s all about the explanation of the approaches to counting files in the directory of Linux.

Note: Both rsync and tree command utilities can be installed on other distributions by replacing the “apt” with their default package manager. For example, in CentOS/RHEL-based distributions, apt will be replaced by “yum”, and in Fedora-based Linux distributions, apt will be replaced with the “dnf” package manager.

How to Count Files in the Directory of Linux Using GUI?

The last but most convenient approach to count the files in the directory is by using the GUI method. In this method, open the directory whose files are supposed to count. For example, we will open the Downloads Directory:

Use the keyboard shortcut key of “CTRL+A”; it will select all the files and show the count details at the bottom of the directory:

The Downloads directory contains the 2 folders and 7 other files.

That’s all from this post!

Conclusion

To count files in the directory of the Linux, we can use the “ls” and “find” with the “wc” command. Additionally, the “tree” and the “rsync” command can also be used to count files in a directory. Apart from the terminal method, a user can utilize the GUI of Linux (where applicable) to count the files in a directory of Linux OS.