How to Count the Number of Files in a Directory in Linux

Managing files is a crucial aspect of working with Linux systems. And while counting files in your Linux system could be one of the tasks that might seem daunting, you can accomplish it with the help of a few useful commands. That said, this article will discuss the different methods to count the number of files in a Linux directory.

Count the Number of Files in a Directory using ls Command

The ls command is the most basic Linux command, which is generally used to list the contents of a directory. You can pair it with the wc Linux command, which can count the number of files in a directory using appropriate flags. The syntax to count the number of files in a directory with the ls command is:

ls <directory_name> | wc -l

For example, to count the number of files in the ~/Documents/test directory, use the following command:

ls -l ~/Documents/test | wc -l
using the ls command to count number of files in a directory

Count the Number of Files in a Directory with tree Command

The tree command is a handy tool and can help to see the directory in a hierarchical form along with the item count. But, this command generally does not come preinstalled on most Linux distros today. This Linux command can also be used to count the number of files in a directory. Use the distro-specific command to install it:

1. Debian-based systems:

sudo apt install tree

2. Arch Linux-based systems:

sudo pacman -s tree

3. Fedora-based systems:

sudo dnf install tree

Once installed, use this syntax to count the number of files in a directory in Linux:

tree -L <depth_level> <directory_name>

In the above syntax, the -L flag is used to specify the depth to which the tree command will look for. For example, to list the contents of the ~/Documents/test directory, use the tree command as described here:

tree -L 1 ~/Documents/test
using the Linux command tree to count the number of files in a directory

Count Files in a Directory with the find Command in Linux

The find command is a multi-utility tool that can be used for various use cases in addition to searching for several items in Linux. When paired with the wc -l command using a shell pipe (|), the find command can be used to count the number of files in any Linux directory. The syntax to count files using the find command is:

find <directory_count> -maxdepth <depth_level> -type f | wc -l

In the above syntax, the -maxdepth <depth_level> refers to the maximum level the find command will search for, and -type f specifies the find command to only look for files. The shell pipe (|) then sends the output of the find command to the wc command where it counts the number of files.

For example, to count the number of files in the ~/Documents/test directory with the find command:

find ~/Documents/test -maxdepth 1 -type f | wc -l
Using the find command to count number of files in a directory

How to Count Files in a Directory in Linux using GUI

The GUI Linux method to count the number of files in a directory is pretty straightforward compared to some of the CLI methods. But, this comes with a trade-off. This method can only work with desktop versions of Linux.

1. Right-click on the directory for which you want to count the files and select the properties option.

opening the properties window for the test directory

2. Here you will see the count of the number of files in this directory.

Properties option to count number of files in a directory
#Tags
Comments 1
Leave a Reply

Loading comments...