Introduction

As a system administrator, it’s important to be aware of how to check the number of files and sub-directories reside within a given directory on your Linux system. Although rare, occasionally you might run into situations wherein the file count in a directory is so high that all the inodes assigned to the file system get exhausted and users will no longer be able to create new files. In this article, we’ll demonstrate a couple of ways we could use to obtain the count of the number of files and directories that reside within a given directory.

Method 1: Using the wc (word count) command
If we want to count the number of files and directories in Linux/UNIX distribution then using the ls command and piping it’s output to the wc command is a simple way to accomplish this. The command ls will list the directory content i.e. the names of the files and directories that reside with the directory and the wc command is used for displaying the word count and when used with the -l option, wc can count the number of lines in the input.

[sahil@linuxnix ~]$ ls
dir1 dir2 dir3 dir4
$ ls | wc -l

While this is good we will not show hidden files or directories. Hidden files start with a dot.  To list these we can use the option -a or -A with ls.  To show all files including hidden files we use the -a option with the ls command and to list all files exclude the . and .. directories we use the -A option with the ls command.  The . and .. directories which are system links with . pointing to the current directory and .. pointing to the directory one level up in the directory hierarchy.

[sahil@linuxnix ~]$ ls -a
. .. .bash_logout .bash_profile .bashrc dir1 dir2 dir3 dir4

[sahil@linuxnix ~]$ ls -A
.bash_logout .bash_profile .bashrc dir1 dir2 dir3 dir4

[sahil@linuxnix ~]$

To obtain a more accurate count of the files and directories, we should omit the . and .. directories from being displayed in the output of the ls command so we’ll use the ls command with the -A option and pipe its output to the wc -l command to display the count.

[sahil@linuxnix ~]$ ls -A | wc -l
7
[sahil@linuxnix ~]$

From the above output we can see that we have a total of 7 items in the current directory.

Method 2: Using the find command
If we want to count directories and files separately then we can use the find command.  To list files we can use the option -type f. In a similar manner, as we did while using the ls command, we would pipe the output generated from the find command as the input to the wc -l command.

[sahil@linuxnix ~]$ find . -type f
./.bash_logout
./.bash_profile
./.bashrc

Listing directories is similar but we will see that we will include the current directory which we may not want.

[sahil@linuxnix ~]$ find . -type d
.
./dir1
./dir2
./dir3
./dir4

To exclude the current directory from the count we can use the option -mindepth 1 to ensure that we start with the directories content and not the directory.

[sahil@linuxnix ~]$ find . -mindepth 1 -type d
./dir1
./dir2
./dir3
./dir4

Method 3: Using ‘ls -ld’ command

We can see that counting files and directories in Linux is not difficult but counting directories can be even easier. The hard link count for a directory can be used to show how many subdirectories there are in the directory. Each subdirectory has a link back to the parent.  A directory starts with a hard link count of 2 so just remove 2 from the current hard link count to see how many subdirectories.

[sahil@linuxnix ~]$ ls -ld /etc
drwxr-xr-x. 112 root root 8192 Jun 8 15:03 /etc

The etc directory has 110 subdirectories on my system, 112 – 2 = 110

So, from here we can also ascertain that if you see a directory that has a hard link count of 2 then it does not contain any sub-directories inside it.

Conclusion

In this quick article, we showed you a couple of ways we could use to count the number of files and sub-directories within a given directory.
We hope that you’ve found the examples to be useful and we look forward towards your feedback and suggestions.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.