Sometimes according to our requirements, we need to move and loop through all the files and directories in a given folder. Let us suppose we want to run a specific command in each folder and file of a directory. For that purpose, we will iterate through all directories using loops. Only a few CLI utilities or command line utilities allow us to run the same command for multiple files. But in our case, we use bash shell scripting and using for loop to speed up any command we want to make our work efficient.
Not only in our case but looping in the whole programming is a very efficient and powerful approach and practice. Because of looping, we can apply the same concept on multiple files and on a group of items using the minimum amount of code to get our desired output. To loop through directories and files in bash, we use a global pattern named “wild card” which is a standard pattern that matches all files. But we will match only directories by using “/” at the end of our command. After that, we assign each directory the value of the available ‘dir’ directory. Finally, we will use the echo command for the loop to run through the directories. Technically, to perform the task, first we have to initialize a variable then we assign the dataset to the variable that we want to loop through. In our case, it cycles through the directories using the ‘*’ Wild card character.
Syntax:
Below is the syntax to loop through the directories.
instruction
done;
In this, we are using the for loop along with the “f” variable that will be created. It defines the action to be performed each time the for loop will be executed. Then, after defining the dataset from which it will go through, we will define a path from which the files are to be fetched or simply use the asterisk “*” which will let the compiler fetch from the current directory. In our case we passed two paths with the name “folder” the “/folder/*” will fetch the files in the folder whereas “folder/**/*” will fetch the files from its subfolders.
Example no. 1:
We will try the method of looping through the directories or files to have a better idea of its work. For that, let us first check for the version of the bash we are having right now because the version should be updated to use the loop. To check the version of bash we will run the command:
It will display the version of the bash that is currently running in our system same as shown below, in our system, it is “5.0,17”.
Now for performing the loop, we will first create a new directory in our system in which we will add some files. To create a new directory, we will run the command:
After running the above command, it will not execute anything, it will simply just hand over the terminal to you to run further commands, as we can see below.
Now, for checking whether our directory is successfully created or not, we will go to the files in our system and then move inside the home directory, you can see the folder named “Linux” created. One question may arise in your mind why we are checking for the folder in the home directory? This is just because we have not provided any path to it, that is why it always creates new folders or files by default in the home directory.
Now, we have to create files in the Linux directory for that we will first move into our Linux directory using the command “cd” as shown below:
After running the command our terminal, it will look like this.
Using the “echo” command, we will create a new file with some dummy text in it by running the following command:
linux@linux-Virtualbox:~ /linux$ echo “my new file 1” > myfile_b.txt
linux@linux-Virtualbox:~ /linux$ echo “my new file 1” > myfile_c.txt
linux@linux-Virtualbox:~ /linux$ echo “my new file 1” > myfile_d.txt
We have created 4 files in our Linux directory naming them “myfile_a”, “myfile_b”,
“myfile_c”, and “myfile_d”, which we can see in the snippet below. There are multiple ways to create new files like using the nano text editor, using the cat command, using the touch command, and many more. It is not mandatory to always use echo, you can use any command you want to.
As we have created our files, it is time to enlist all files using for loop. To perform this, we will run another command.
In the command above, we created a variable “f” which will define the action that will be taken on the execution of the loop. In our case, we are displaying the file names that are stored in the Linux directory as we can see in the snippet below.
By using the command above, we have displayed the file names that are stored in the Linux directory. Let us suppose we want to display the content along with the file name, it can also be done using for loop instead of writing the echo command again and again.
As shown below, the content of all files is displayed below separated by the new line. In the command above, we created a variable “f” it is not necessary to create the same variable. You can create any variable according to your need it is not compulsory to use “f”.
Conclusion
Today we studied looping through the directories, we introduced you to the for loop that is used to fetch files from directories and also with its works. We explained to you the method to use “for loop” for fetching the directories using a simple example in which we first created a new directory and files, then we simply accessed them using “for loop”.