Bash script to get total number of Directories in Linux

 This bash script calculates the total number of directories including the sub-directories in the current directory but does not count the current directory. Before I proceed to the bash scripting let me explain the bash script.

What is Bash Script?

Bash script is a list of commands that are executed in order when the script is run. The bash script is a plain text file containing a series of commands or a series of commands written in a file and executed by a bash program. Any command that can be run on the terminal can also be written into a bash script and perform precisely the same thing. 

Bash scripts can also be used to automate tasks, perform system administration tasks, and many other tasks. The bash script file extension is .sh, even though the bash script can run fine without adding a .sh file extension but make sure you add the shebang.

What is a shebang?

Shebang is a character sequence consisting of characters, starting with a sharp sign and an exclamation mark (#!) at the beginning of the bash script. Shebang can also be called sharp exclamation, sha-bang, or hashbang. Shebang is the combination of bash (#) and bang (!) follow by the bash shell path. Shebang is a path to the bash interpreter, that tells the shell to execute it via the bash shell.

Below is an example of a shebang statement;

#! /bin/bash

In order to get a total number of directories in Linux, we will need to make use of two different Linux commands. first will have to execute and afterward pass its outcome as the input for the second command.

So, we need to pass the output of the first command as input for the second command by using a concept called Pipping. Pipping involves the use of pipelines | in between two commands or more, whenever a pipe | is involved in between two commands, the output of the first command is used as an input to the second command.

first_command | second_command

We will need two commands, the find command, and wc (word count) command. we will be using the find command to search (find) and list all the subdirectories within the current directory, we are going to pass on the result of the first command to the second command (wc command) to count the total number of directories present.

The find command can search and list any file, including both directories and non-directories. So, we need to restrict the search to only directories by adding an attribute to modify it. we can set file commands on what it needs to search and when it needs to stop, They are various to achieve with the find command depending on what we intend to achieve. Let's look at the file command available options available

OPTION 1: Check only Directories 

To modify the find command to search only directories, we will need to use the -type option, followed by a d attribute to indicate its directories. See the command below;

find -type d

OPTION 2: Deep Search 

According to the article topic, we are meant to find the total number of the directories and the sub-directories in the current directory, that's when the deep search comes in by using the depth attribute. we need to start the depth as a minimum as it can to get any present directory in the current directory. 

Depending on what we intend to get, we can also use the maximum attribute but on this question, we will be using the minimum depth. See the command below;

find -mindepth 1

If we go ahead and merge both options together the code will list the directories and their respective subdirectories in the current directory. See the code below;

find -mindepth 1 -type d

After the above code has listed the directories and subdirectories we can now pass the output to the wc command. wc command has many functionalities like counting the number of lines, the number of words, and the number of characters. On this question, we will be using the number of lines. wc command follows by the -l attribute, so we can print the total number of lines of the find command output. see the command below;

wc -l

With the help of pipe |, which we talk about earlier, when the commands come together the final code will be;

find -mindepth 1 -type d | wc -l

With what has been explained so far, I hope you should be able to understand Bash scripting and how to get the total number of directories and their respective subdirectories without any issues or problems.

Previous Post Next Post