count files in directory

How to Count Number of Files in Directory in Linux

System administrators often need to keep track of the files in their system. So they need to regularly count number of files in directory in Linux. There are several utilities to help you count files in directory in Linux system. In this article, we will learn the different ways to count number of files that are stored in a directory in Linux.


How to Count Number of Files in Directory in Linux

Here are the different commands to count number of files in Linux.


1. Count Files using wc

The simplest way to count files in a directory is to pipe the output of ls command to wc command. wc -l command is generally used to count the number of lines in file or input. Since the output of ls command lists all files when you pass it to wc command, it will end up counting the number of files in your directory. Here is an example.

$ ls | wc -l

The above command will display the count of files in your present working directory. If you want to count the number of files in a specific folder such as /data, just mention it after the ls command, as shown below.

ls /data | wc -l

if you want to count a specific type of file, you can do so using wildcard characters after ls command. Here is the command to count only pdf files in your present working directory.

$ ls *.pdf | wc -l


2. Count Files Using Find

You can also pipe the output of find command to wc -l command to get count of number of files in directory. Here is the syntax for the same.

$ find <directory> -type f | wc -l

Here is an example to count all files in folder /data.

$ find /data -type f | wc -l

But please note that find command counts files recursively in all subfolders in the specified folder. If you don’t want a recursive search, use maxdepth option as shown below.

$ find /data -maxdepth 1 -type f | wc -l

Another point to remember is that the output of find command will also display all the error messages encountered during execution. For example, if you don’t have required permission then, it will count the error messages as part of wc command. To avoid it, you can redirect error messages to /dev/null as shown below.

$ find /data -maxdepth 1 -type f 2>/dev/null | wc -l

Now the above command will count the actual files you have access to, and exclude the ones you don’t have access to.


3. Count Files Using Tree

You can also use tree command to count number of files in folder. Here is the command to install tree in your Linux system.

$ sudo apt-get install tree             (for Ubunbu/Debian hosts)
$ sudo yum install tree                 (for CentOS/RHEL hosts)

Here is an example to count the number of files in a directory /data.

$ tree /data
3 directories, 3 files

The above command will only count visible folders and files, and not hidden ones. In order to count hidden files & folders, use -a option.

$ tree -a /data


4. Cronjob to count files

You can also create a cronjob to regularly count the number of files in a directory and save the output to file. For this purpose open crontab with the following command.

$ crontab -e

Add the following line to count number of files in /data and save the output to file_count.txt file

0 10 * * * sudo ls /data | wc -l > file_count.txt 2>&1

The above cronjob will run everyday at 10.a.m. Now you don’t have to manually run the commands to count files. You can simply open file_count.txt file to view the number of files present on that data.

Similarly, you can also add the above commands to shell script, if you want to automate the whole process.

In this article, we have learnt several ways to count number of files in folder. You can customize these commands as per your requirement.

Also read:

How to Increase Your Security on the Internet
How to Encrypt Partition in Linux
How to Encrypt Folder in Linux
How to Use SSH Instead of HTTPS in Git
How to Encrypt File in Linux

Leave a Reply

Your email address will not be published.