How to Count Files in a Directory in Linux?


Whenever you want to count the files in a directory, you might just visit that directory and start counting the files residing inside it manually. However, when the number of files is very large, then it is not feasible to count them manually. Therefore, in this tutorial, we will be explaining to you the different methods of counting the files in a directory with simple one-liner commands in Linux Mint 21.

Methods of Counting the Files in a Directory in Linux Mint 21:

If you wish to count the files in a directory of your Linux Mint 21 system, then depending upon your exact intent, you can use any of the following four methods:

Method # 1: Listing the Total Number of Files and Sub-Directories only at First Level:

When you want to count the total number of files and sub-directories only at the first directory level, then you have to run the command shown below:

$ ls -1U Sample | wc –l

Here, “Sample” represents the name of our target directory. The total number of files and sub-directories at the first level of this directory is shown in the following image:

Method # 2: Skipping the Sub-Directories and Only Listing the Total Number of Files at the First Level:

Now, if you want to display the total number of files present on the first level of a directory while skipping the sub-directories, then you have to modify the above-mentioned command as shown below:

$ ls -1Up Sample | grep –v / | wc –l

The total number of files present at the first level of our specified directory is shown in the following image:

Method # 3: Recursively Counting the Total Number of Files:

If you want to count the total number of files recursively i.e. finding all the files while digging down to all the sub-directories of a directory, then you have to run the command shown below:

$ find Sample –type f | wc –l

This command will display the total count of files after recursively running through all the levels of the specified directory as shown in the image that follows:

Method # 4: Displaying the Total Number of Files and Sub-Directories:

Finally, if you intend to count the total number of files and sub-directories at all the levels of a directory, then you have to execute the command shown below:

$ tree Sample

This command will display the complete tree-like structure of the specified directory and in the end, it will also display the total number of files and sub-directories as highlighted in the following image:

Note: To use this method, you must have the “tree” command installed on your Linux Mint 21 system. If it is not already installed on your system, then you can simply install it by executing the “sudo apt install tree” command on your system.

Conclusion:

This article discussed the different methods of counting the files in a directory in Linux Mint 21. Now, you can use any of these methods after identifying the level of the directory at which you wish to count the resident files.

For more how-tos and tutorials, please visit Linux Genie.

Print Friendly, PDF & Email
Categories