How to Count Files in Linux Directory?
If you are a System Administrator or a Server Administrator, you are constantly observing the files on the system. This includes managing and maintaining the files and sorting and counting them.
This post will act as a guide and show you all the different ways that you can use to count files in a linux directory.
Table of Contents
The main command in most methods will be the wc command which will be combined with other commands and tools to count the number of files in a Linux Directory. Let’s get started with the first method.
Method 1: Using the wc and ls Command to Count Files in Linux
The easiest way to count the number of files in a directory is to use the wc command with the ls command. The wc is used to count the number of lines, and when combined with the ls (list) command, it prints out the number of lines in the output of the list command.
To demonstrate its working, type the following command in the terminal of a directory:
ls | wc -l
This will print out the following output on the terminal:
The output shows that there are a total of 11 files in the working. However, if you use the ls command with the -l flag, then you will get one extra count. To check this, type the following command in the terminal:
ls -l | wc -l
This time around, this will display the following output on the terminal:
The output says “12”, but the number of files in that directory is 11 because this command will always show one more than the total number of files.
Method 2: Using the find Command to Count Files in Linux
The second approach is utilizing the find command to count the files in a Linux directory. The find command is a command line utility. When used with the “-type f” flag and the wc command, it will show the number of regular files in the directory.
To demonstrate this, open up a terminal in a Linux Directory and then type the following command inside it:
find . -maxdepth 1 -type f | wc -l
Doing this will return the following output on the terminal:
However, with this approach, you will not be able to count directories within a directory and the files in the sub-directories.
How to Count Files Within Subdirectories With the Find Command
If you want to count the number of regular files within the parent directory and its subdirectories, you can use the find command for recursive search. To do this, you can use one out of two different approaches. The first one to use the following command:
find . -type f | wc -l
With this command, you are not defining depth, making the command recursive and executing for all subdirectories of the current working directory.
The approach is to specify the directory path with the find command. To do this, use the following syntax:
find <directory> -type f | wc -l
For example, if you want to search for /Desktop, then the above command will become:
find ~/Desktop -type f | wc -l
This will result in the following output:
Method 3: Using the tree Command to Count Files in Linux
You can also use the tree command-line utility to print the number of files in a Directory. However, the tree is not a built-in command. Therefore, you might have to install it before actually using it. To install the tree tool, type the following command in a terminal session:
sudo apt-get install tree
Once you have installed the tree utility, open the directory a terminal session in the directory in which you want to count the files, and then simply type the following command:
tree
This will give you the following output:
In the output, you can see that this command shows the names of the files, and at the very last line, it shows the total number of directories and files found.
How to Check Tree of a Specific Directory With the tree Command
With the tree command, you can also check the files of any specific directory. For example, if you want to check the tree of Desktop, then you can use the following command:
tree ~/Desktop
This will show you the following output:
This shows that there are a total of 3 directories on the Desktop and a total of 25 regular files within those directories.
How to Count Hidden Files With the tree Command
You can also use the tree command to count the hidden files using the tree command. To do this, add the -a flag and then specify the directory. The syntax for doing this is as follows:
tree -a <directory>
Affordable VPS Hosting With Dracula Servers
Dracula Servers offers high-performance server hosting at entry-level prices. The plans include Linux VPS, Sneaker Servers, Dedicated Servers & turnkey solutions. If you’re looking for quality self-managed servers with high amounts of RAM and storage, look no further.
Dracula Server Hosting is also Perfect for Hosting Telegram.Forex App with built-in support for MT4 with trade copier. Check the plans for yourself by clicking Here!
Method 4: Using the GUI to Count Files in Linux
If you are a new Linux user, you prefer to use GUI more often than the command line utilities. If that is true, you can easily find the total number of files inside a directory using two different methods. However, these methods differ depending on the type/variant of Linux you use.
How to Count Files in Linux Using Properties GUI
For this, simply open up the files explorer and head inside the directory with the files to be counted. Right-click in the explorer and select the properties option:
This will open up a new dialogue box that will contain the information about the directory. Inside here, you will also find the total number of files available in that directory:
You can see from the image above that there are a total of 11 items and 154.1 MBs.
How to Count Files in Linux Using GUI Quick Select
Another quick method of displaying the number of files present inside a directory is to use the “select all” option. However, this may not be the case for all variants of Linux.
To use this, open the file explorer and head inside the directory. After that, press “CTRL + A” to select all the content of that directory. This option will display a small prompt on either side of the file explorer:
You can clearly see that there is a total of 11 items of 154.1 MB.
Summary
You can easily count the number of files in a Linux directory by using the wc command with different command-line utilities like ls, find, and tree. For the tree command, you will have to ensure that it is installed in the system. After that, in all of these commands, you can also use the -a flag to count the hidden files. If you don’t prefer the use of commands and command-line utilities, then you can also use the GUI options depending on the Linux Variant.