How to Count Files in a Directory in Linux?

Count Files Featured Image

In this tutorial we will be learning various ways to count files in a directory. One of the most annoying features of using a computer system is managing space on the system. With the presence of numerous directories, navigation through each of them is time-consuming. This menial task of counting files in a specific directory can be fulfilled by certain commands.

1. Count the Number of Items in a Directory

In order to display the count of items immediately present inside, we will use a combination of two commands:

ls <PATH> | wc -l
Count Files Item Count
Total item count in a directory

The ls command is used to display the contents of a directory, whereas the wc command is used to count words or lines in a certain text. In the example above, the '~' symbolizes the home directory.

Note: If the user has to count items in the current directory, then the '<PATH>' can be omitted in the above command.


2. Count Files in a Directory

It is quite clear that a directory might contain files, folders, or links. There can be special ways to only count files in a directory. It is done by:

ls -l <PATH> | grep "^-" | wc -l
Count Files Only Files
File count in a directory

Let us try to understand the process of this command. The 'ls -l ~' displays the elongated format of file contents.

Count Files Ls L Edited 1
Output of ‘ls -l ~’

There is a vivid pattern for files that we can notice from this output. All the regular files start with '-', whereas directories start with 'd'. This pattern can be matched using Linux commands.

The grep command filters out the lines that start (denoted by '^' symbol) with '-'. The filtered text is passed to the wc command which prints the number of lines given as input.


3. Number of files in the entire directory

The above commands only count items in the immediate directory. Sometimes, we need to count the files in directories within a directory. This type of count can be achieved by a few Linux commands.

Using the find command

The find command does exactly what it means. It is used to find and filter files or directories in Linux systems. In order to get a total count of files inside a directory (including files inside sub-directories).

find <PATH> -type f | wc -l
Count Files Find Command
Total count of files

The use of '-type f' option tells the command to list the files in that directory. The list of files is passed as a text to the wc command, which counts the number of lines in it.

Note: There can be certain sub-directories where a non-root user might not have access. Therefore in that case, sudo must be placed before the find command.


Using the tree command

The tree command is used to display the directory tree along with the final count of files in it. It is not a preinstalled command in Linux, therefore needs to be installed before using it.

Ubuntu/Debian users can install it by:

sudo apt install tree

Users of other Linux distributions can install it using their standard installation command followed by tree.

After the installation is complete, we need to simply run:

tree <PATH>
Count Files Tree Command
Count files using tree command

As we can see from the image, the command first displays the tree-like directory structure and then displays the final statistics. There are 31 directories and 416 files inside the given directory.


Using the rsync command

The rsync command is mainly a file backup tool. It can be used to display files as well as certain statistical information with respect to a directory.

Since we need a count of files and not a files backup, we must use '--dry-run' option with the command.

rsync --stats --dry-run -a <PATH>
Count Files Rsync Command
Count files using rsync command

The '--stats' option is used to display the end statistics, whereas the '-a' option informs the command to considers the files inside sub-directories as well. The first line of the statistics display total items in the entire directory followed by number of regular files and number of sub-directories.

This sums up the ways of counting files in a directory using Linux commands.


Conclusion

In this article, we understood various ways of counting files in a directory in Linux. We hope each method provided was easy to follow by the reader. In case, we missed any technique, feel free to comment below.