1. Overview

In this tutorial, we’ll learn how to count the number of directories in a specific directory. While the problem looks easy, there are a few things to keep in mind. We’ll explore various techniques to solve this problem.

2. The Sample Directory

Let’s see the list of directories, sub-directories, and files within the sample directory we’ll be using. We can do this with the help of the tree command:

$ tree sample
sample
├── test1
│   └── test5
├── test2
├── test3
├── test4
└── test_file1.docx

5 directories, 1 file

Our sample directory contains four directories: test1, test2, test3, test4, one file: test_file1.docx, and one sub-directory to the test1 directory: test5. So, there are a total of five directories within the sample directory: test1, test2, test3, test4, and test5.

3. Counting the Number of Directories

Let’s now discuss some common solutions for the problem of counting the number of directories within a directory.

3.1. Using the find Command

The find command finds directories and files on a filesystem and carries out actions on them.

Let’s see how to get the count of the number of directories within a directory using the find command (recursive search):

$ find . -type d | wc -l
   6

The find command above finds directories in the current directory. Here, the dot (“.”) refers to the current directory, and -type d means that we’re only interested in finding directories and not regular files.

The wc command means word count. In the above example, the find command redirects its output to the standard input of the wc -l command, which performs a line count on that input.

Now, as we noted above, our sample directory contains five directories. However, the output of the above command shows the count of directories as six. Why the difference? Simply put, it counts six directories because the find . -type d command also outputs the current directory:

$ find . -type d
.
./test1
./test1/test5
./test4
./test3
./test2

We can run the find command with the -mindepth 1 option to exclude the current directory from the output:

$ find . -mindepth 1 -type d | wc -l
    5

By using the –mindepth 1 option, it finds all directories except the current directory, yielding our desired result of 5.

Now, suppose we only want to count the number of directories directly within our sample directory without considering any nested subdirectories — a non-recursive search. How can we achieve this with the find command?

Actually, it’s quite simple to achieve:

$ find . -mindepth 1 -maxdepth 1 -type d | wc -l
    4

The output of the above command shows the count of directories as four and not five. Why the difference? The find command with option -maxdepth 1 sets the total number of recursive in directories to 1.

This means only the immediate subdirectories of the current directory are considered. In our example sample directory, the test5 directory (which is within the subdirectory test1) is not considered in the count of directories.

3.2. Using the ls Command

The ls command lists the directories and files contained in a directory.

Let’s explore how to get the total number of directories in the current directory using the ls command. First, we’ll do a recursive search, which includes nested subdirectories:

$ ls -lR | grep ^d | wc -l
  5

The ls command with the -lR options displays the list (in long format) of the sub-directories in the current directory recursively.

Then, we use the grep command to search for a string or pattern in the input. In the above command, the ls command lists the contents of all directories. Here, we’re piping (“|”) the output of the first command (ls -lR) to the input of the second command (grep ^d). Again, we’re piping (“|”) the output of the second command (grep ^d) to the input of the third command (wc -l).

The grep command matches the pattern “d” at the beginning of each line on the input. The caret (“^”) refers to the start of the line.

And again, wc -l counts the number of lines.

If we want to get the total number of directories excluding nested subdirectories (non-recursive search), we only need to remove the -R flag from the ls command we used above:

$ ls -l | grep ^d | wc -l
   4

3.3. Using the tree Command

The tree command is a Linux program to list directories and files in a tree structure.

Let’s explore how to get the total number of directories in a directory using the tree command (recursive search):

$ tree | tail -1
5 directories, 1 file

The tree command displays the depth of the current directory tree recursively.

The tail command writes the last ten lines of an input file to the standard output. In the above example, the tree command lists all the directories in the current directory. Here, we’re piping (“|”) the output of the first command (tree) to the input of the second command (tail -1).

The tail command with the -1 option prints the last line on that input:

$ tree
.
├── test1
│   └── test5
├── test2
├── test3
├── test4
└── test_file1.docx

5 directories, 1 file

Now, let’s use the tree command to perform the non-recursive search:

$ tree -L 1|tail -1
4 directories, 1 file

The output of the above command shows the count of directories as four and not five. Why the difference? The tree command with option -L 1 sets the display level of the directory to 1.

In our sample directory, the test5 directory (which is within the sub-directory test1 at level 2) is not considered in the count of directories.

The tree command is not installed in every Linux system by default, but we can easily install it using yum or apt-get if it’s not present on our system.

4. Conclusion

In this article, we learned how to count the number of directories in a specific directory using multiple commands like find, ls, and tree. In addition, we saw how to perform a recursive search (including all nested subdirectories) and a non-recursive search with each command.