Bash Loop Through Files In A Directory?
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.
for f in / folder /* folder /**/* ; do
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_a.txt
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”.
About the author
Omar Farooq
Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.
How To Loop Through Files in a Directory
Not all command-line utilities allow you to run a command against multiple files, but with the power of shell scripting and the for loop, you can super charge any command you choose.
Looping is one of the most powerful things you can do in programming. It allows you to apply the same logic over and over to a group of items with minimal code.
In this tutorial you’ll iterate over files and apply commands using either the Bash or zsh shells.
Step 1 — Looping Through Files
You are going to use the for shell scripting loop in this tutorial. This particular control flow method is baked into the Bash or zsh command-line shell.
You can apply the following commands to any directory of your choosing, but for this tutorial, create a directory and some files to play around with.
First, create the directory:
Then switch to the new directory:
Now use the touch command to create a few text files:
You can also create these files quickly using brace expansion and a range:
To loop through a directory, and then print the name of the file, execute the following command:
You’ll see the following output:
Outputfile-1.txt file-2.txt file-3.txt file-4.txt file-5.txt
You probably noticed we’re using the wild card character, * , in there. That tells the for loop to grab every single file in the directory. You could change the wild card could to file-* to target all of the files that started with file- , or to *.txt to grab just the text files.
Now that you know how to loop through the files in a directory and spit out the names, let’s use those files with a command.
Step 2 — Applying a Command
The files you created in the previous section were all created with the touch command and are empty, so showing out how to cat each file wouldn’t be very useful.
Fortunately, using a similar for loop, you can insert text into each file with a single command.
Execute the following command to insert the file’s name, followed by a newline, followed by the text Loops Rule! into each file:
The -e flag on echo ensures that newlines are preserved.
The exclamation point needs to be escaped with a backslash so the shell doesn’t interpret the character as a shell command.
Now iterate through each file and print its contents:
Now each file contains the name of the file on the first line, and a universal truth about loops on the second line.
Outputfile-1.txt Loops Rule! file-2.txt Loops Rule! file-3.txt Loops Rule! file-4.txt Loops Rule! file-5.txt Loops Rule!
To take it a step further, you could combine these examples to first write to the file, then display its contents in a single loop:
You’ll see the following output:
Outputfile-1.txt Loops Rule! file-2.txt Loops Rule! file-3.txt Loops Rule! file-4.txt Loops Rule! file-5.txt Loops Rule!
By separating the commands with a semi-colon, ; , you can string together whichever commands you need.
Now let’s look at a practical example: making backup copies of files.
Step 3 — Making Backups of Files
Now that you know how the for loop works, let’s try something a bit more real world by taking a directory of files and making backups that are suffixed with the .bak extension.
Execute the following command in your shell which creates a backup for each file:
Now use the ls command to display each file:
You’ll see the following output:
Outputtotal 40K -rw-r--r-- 1 sammy sammy 29 Nov 7 18:34 file-1.txt -rw-r--r-- 1 sammy sammy 29 Nov 7 18:41 file-1.txt.bak -rw-r--r-- 1 sammy sammy 29 Nov 7 18:34 file-2.txt -rw-r--r-- 1 sammy sammy 29 Nov 7 18:41 file-2.txt.bak -rw-r--r-- 1 sammy sammy 29 Nov 7 18:34 file-3.txt -rw-r--r-- 1 sammy sammy 29 Nov 7 18:41 file-3.txt.bak -rw-r--r-- 1 sammy sammy 29 Nov 7 18:34 file-4.txt -rw-r--r-- 1 sammy sammy 29 Nov 7 18:41 file-4.txt.bak -rw-r--r-- 1 sammy sammy 29 Nov 7 18:34 file-5.txt -rw-r--r-- 1 sammy sammy 29 Nov 7 18:41 file-5.txt.bak
You now have exact copies of each of your files, with an extension that indicates that they are backup files.
You’re not limited to making copies in the same directory either; you can specify a new path for your backup files. The following command stores the backups in the folder /tmp/my-backups , provided the directory already exists:
The backups are created in the new location.
Conclusion
In this tutorial you experimented with the for loop in your shell and used it to iterate over files and apply commands.
Spin up a real linux environment on a hosted virtual machine in seconds with DigitalOcean Droplets! Simple enough for any user, powerful enough for fast-growing applications or businesses.