How to Count Files in a Directory in Linux?

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.

How can I count the number of files in a directory on Linux?

You can count the number of files in a directory on Linux by using the `ls` command with the `wc` command. For example, the following command will count the files in the current directory: `ls | wc -l`.

How can I count hidden files using Linux commands?

To count hidden files in a directory, we’ll explore the `ls -a` command combined with `wc` for accurate results. The command `ls -a | wc -l` will count all files, including hidden ones, in the current directory.

Is there a way to count files and directories recursively in Linux?

Yes, you can recursively count files and directories using the `find` command. The command `find . -type f | wc -l` will count all files in the current directory and its subdirectories.

Can I count only files in a specific directory without including subdirectories?

Yes, you can count only files in a specific directory without including subdirectories by using the command `ls -l | grep -v ‘^d’ | wc -l` which filters out directories.

How do I count files in a directory using a bash script?

You can create a bash script to count files in a directory by using the following code: “`bash #!/bin/bash echo “Number of files: $(ls | wc -l)” “` This script will display the total number of files present in the current directory.

How can I include hidden files in my file count using a script?

To include one hidden file in your file count using a script, modify the `ls` command to `ls -a | wc -l` in the bash script to get started.

Are there graphical interface options to count files in Linux?

Yes, graphical interfaces like KDE’s Dolphin or GNOME’s Nautilus allow you to view the count number of files within their properties or status bars, which can also include subdirectory counts. You can navigate to the directory of interest, and the interface will often show the total number of files and directories.

What are the prerequisites to count files in a directory using the command line on Linux?

The main prerequisite is to have access to the terminal as a Linux user and know basic command line navigation to execute commands within the desired directory.

How can I count files in a directory without using the terminal?

You can use a graphical file manager like Dolphin or Nautilus, which provides a user-friendly interface to view the total number of files and directories in any selected folder without needing to operate in the terminal.