Thursday, July 21, 2022

3 Examples of for loop in Linux and Bash Script [Tutorial]

Hello guys, if you want to learn how to use for loop in bash or Linux then you have come to the right place. Earlier, I have shared free Linux courses and free bash scripting courses, and today, I am going to share three simple examples of using for loop in Linux.  If you are a Programmer, Software Engineer, or System Administrator working in a UNIX or Linux environment, then you will probably find the shell 'for' loop to be a handy tool for automating simple command-line tasks. This is the single command which has helped me a lot while doing production support and performing operational tasks. 

To give you an example of what for loop in bash shell can do for you let's think that you have 10 servers in production and if you want to copy one file to all the servers then instead of manually running the SCP command 10 times, you can quickly write a four-line script using for loop to copy into all those 10 servers. 

Not only it will take less time but also you can reuse them and extend them to the number of the server you want. That's where for loop comes into the rescue. You can quickly put the code you want to execute inside the for loop and done. You have automated it and you not only save time but also have a bash script that you can use next time. 

Btw, if you are new to Linux then I also suggest you first go through comprehensive and hands-on Linux courses to learn basics and essential commands. If you need a recommendation, I highly recommend you to check Linux Mastery: Master the Linux commands in 11.5 hours on Udemy. It's the highest-rated Linux course on Udemy and very useful to learn essential Linux commands. 







3 Examples of for loop in Linux and Bash Shell Scripting

Without wasting any more of your time, here are 3 common examples of using for loop in bash and Linux to automate simple tasks. 

1. Rename all ".old" files in the current directory to ".bak":

This is one of the common examples of using for loop to move all files with a particular extension into another directory. 
for i in .old 
 do
 j=`echo $i | sed 's/old/bak/'`
 mv $i $j
 done 

Here, we looped through all files with extension ".old", setting the variable "i" to be the file name we are currently looping through. Then, between the "do" and "done", we have the body of the loop. On each pass, we echo the file name ("i") to the UNIX stream editor sed.

Sed replaces the "old" with "bak" (so file "a.old" becomes "a.bak"), and saves the changed name to variable "j". Then, we use the UNIX move (mv) command to rename the original file (ex. a.old) to the new file (a.bak).

2. Change all instances of "yes" to "no" in all ".txt" files in the current directory. Back up the original files to ".bak".

In this example, we rename each file from ".txt" to ".bak". Additionally, we use sed a second time, on the contents of the original file (now with a ".bak" extension), and save the modified text back to the original name (with ".txt").

for i in .txt
 do
   j=`echo $i|sed 's/txt/bak/'`
   mv $i $j
    sed 's/yes/no/' $j > $i
 done 

You can see we are using the mv and sed command inside for loop to automate tasks. Btw, if you are not familiar with basic Linux commands like grep, find, chmod, etc. then I suggest you first go through a basic course like Linux Command Line Basics on Udemy. This will help you to learn more in a short time.

How to use for loop in Linux



3. Loop through a text file containing possible file names. If the file is readable, print the first line, otherwise print an error message:
Here is another example of for loop or for command in bash. In this example, we loop through the results of a command (in this case "cat"), rather than looping thru files in the directory. 

You can also use an if statement with the "test" command to test for a condition (in this case, whether the file is readable).
for i in `cat file_list.txt`
 do
     if test -r $i
     then
           echo "Here is the first line of file: $i"
           sed 1q $i
     else
         echo "file $i cannot be open for reading."
     fi
  done


That's all about how to use for loop in a bash script to automate tasks. I am saying this from my personal experience that learning for command is the best thing I did. It allowed me to write a powerful shell script that I couldn't have written before. It's best for automation, you can deploy builds to multiple servers, copy log files from multiples and even delete temporary files from multiple servers. 



Other UNIX command tutorials you may like to explore
  • 10 examples of lsof command in Linux? (examples)
  • VI Editor examples and tips for beginners (example)
  • Top 5 Bash Scripting course for Beginners (bash courses)
  • 5 Example of kill commands in Unix and Linux (example)
  • Top 10 Courses to Learn Linux commands (courses)
  • 10 example of the networking command in Unix (example)
  • My favorite Linux courses for beginners (Linux online courses)
  • 10 Example of tar commands in Unix (example)
  • 6 Free Online course to learn Bash (free course)
  • How to use the netstat command to find which process is listening on a port? (example)
  • How does nslookup command work in UNIX? (answer)
  • 10 books to learn essential commands of Linux or UNIX? (list)
  • How Linux works? What Every SuperUser should know? (book)
  • 10 Examples of cURL command in Linux? (examples)
  • How to use the CUT command in Linux (tutorial)
  • How to exit from the telnet command in UNIX? (example)
  • How to send an HTTP request to call a web service from the command line in Linux? (example)

Thanks for reading this article so far. If you like these for loop example in bash and Linux, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a comment.

P. S. - If you are looking for some free courses to learn or improve your Linux skills, you should check out this Introduction To Shell Scripting  (FREE Course) on Udemy. This course is completely free and more than 750000 students have already joined this free course. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.