How To Count Files And Directories in Linux

Being able to count files in a directory on Linux, or directories themselves, is a useful admin task to know how to carry out. It may be that you are troubleshooting disk space issues or it could be that you are investigating a problem with log file rotation. There are many reasons why you may want to count files in a directory or to count the number of directories.

This article looks at some of the ways, and tools, you can use to count files in a directory on Linux. Note that many of the techniques here also will work to count files on VMware ESXi.

We will start by looking at the most common way to count files on Linux, the wc command.

Count Files in Current Directory using WC

The wc command is available on all Linux distributions, so is usually the go to command for counting files and directories. As the man page states, wc is used to print newline, word, and byte counts for each file. We can get wc to take it’s input from the ls command, which is used to list the contents of a directory. For example, I have a directory with 26 files, I can use the ls command to list them:

$ ls
filea.txt  filed.txt  fileg.txt  filej.txt  filem.txt  filep.txt  files.txt  filev.txt  filey.txt
fileb.txt  filee.txt  fileh.txt  filek.txt  filen.txt  fileq.txt  filet.txt  filew.txt  filez.txt
filec.txt  filef.txt  filei.txt  filel.txt  fileo.txt  filer.txt  fileu.txt  filex.txt

We can count the number of files in the directory by passing the output, or pipe it, to the wc command:

$ ls | wc -l
26

As you can see, it has returned 26 as the number of files. This is because wc using the -l option has counted the number of lines in the output of the ls command.

Note: It’s important that certain switches are not used with the ls command to list the number of files. Adding, for example, the -lah or -ls options will increase the number of lines in the output of the command, which will result in a misleading value for the file count.

You can get a count of files in other directories by adding the full path to the ls command. For example:

$ ls /home/cloud_user/files | wc -l
26

How to Count Hidden Files

While this works well, it will not show hidden files, as ls by default will not display them in it’s output. Hidden files and directories on Linux are prefixed with a dot. For example:

-rw-------  1 cloud_user cloud_user 3.6K Jan 11 10:35 .ICEauthority
-rw-------  1 cloud_user cloud_user 4.3K Jan 11 10:35 .Xauthority
-rw-------  1 cloud_user cloud_user  407 Jan 11 12:47 .bash_history
-rw-r--r--  1 cloud_user cloud_user  220 Oct 17  2018 .bash_logout
-rw-r--r--  1 cloud_user cloud_user 3.7K Oct 17  2018 .bashrc

If we want to include hidden items in the count of files in the directory we can use the -A option with the ls command:

$ ls -A | wc -l
29

That’s how you can count hidden files on Linux.

Counting Files in Real Time

If you have a program or script that is creating files or logs, you may want to monitor the number of files in a directory in real time, so that you can see how quickly files are being added or removed. One way to do this is to use the commands we have looked at so far, along with the watch command. For example:

$ watch 'ls -A | wc -l'

The watch command will continuously run the la -A | wc -l command (every 2 seconds by default), displaying the output each time it refreshes. As such you can monitor the file count:

I’ve written about using this to monitor virtual machine disk files on VMware ESXi.

Let’s now move on to looking at how we can count files and directories using a different tool, called find.

Count Linux Files and Directories using Find

Like wc, the find command should already be present on your Linux build. As the name suggests, find is used to find files or folders, but it can also output a count of the files or directories it finds if we use it with wc. Unlike with the ls command, find is able to count files recursively, so that we can get a count from any sub-directories the directory may contain.

To count all files in the current directory and any directories it contains, we can use the -type f option with the find command, which will list all files it finds. As before, we want to pipe the output to the wc command:

$ find . -type f | wc -l

To count files in another directory, simply replace the '.' with the full path. For example:

$ sudo find /etc -type f | wc -l
882

This shows us that there are 882 files in the /etc directory. Note that I used sudo this time to ensure I had enough permissions to read the contents of /etc without receiving any errors.

How to Count Directories using Find

To count directories, rather than files, use the -type d option with the find command:

# find /etc -type d | wc -l
254

As you can see, there are 254 directories in the /etc directory. If you wish to count files and directories you can combine the two types in the find command:

# find /etc -type f,d | wc -l
1136

We have 1136 files/directories in total, which is the sum of the earlier commands (882+254). Good news!

OK, so far we have looked at how to count files using Linux command line tools. The combinations of ls and wc and find and wc will have you covered for most of your Linux file counting needs.

However, you might want create a script to count Linux files, either to create a task that is easily repeatable, or to give you more control over the output. I write the occasional article on Python, so let’s have a quick look at how to count files using Python.

How to Count Linux Files using Python

To interact with the Linux file system using Python we can use the os module. A simple script to count the files in a directory using Python could look like this:

import os

path, dirs, files = next(os.walk("/home/cloud_user/files"))
file_count = len(files)
dir_count = len(dirs)
print "There are", file_count ,"files in this directory."
print "There are", dir_count , "directories."

First we are importing os, then using it to count the files and directories in the /home/cloud_user/files directory, and saving the length of the output to a variable for each. Finally, the print command is used to create the output from the script. The result is:

# python filecount.py
There are 30 files in this directory.
There are 3 directories.

Hopefully that should give you an idea of how you can create a python script to count files on Linux!

Conclusion

In this tutorial you have learnt how to count files and directories using the ls, wc and find commands, which are available on all Linux systems.

We have also taken a look at how you can use the watch command to monitor file counts in real time.

Finally, we had a quick look at how you can use Python to create a script that will count files and directories.

Related posts

Mastering the Linux ifconfig Command

Docker Exec Command With Practical Examples

Debugging with Git Bisect

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More