How To Linux Bash For Loop In Files? – POFTUT

How To Linux Bash For Loop In Files?

Bash provides a lot of useful programming functionalities. for loop is one of the most useful of them. We can use for loop for iterative jobs. Linux system administrators generally use for loop to iterate over files and folder. In this tutorial, we will look at how to use for loop to iterate over files and directories in Linux. This example can be used any of Linux distribution which uses bash as shell-like Ubuntu, CentOS, RedHat, Fedora, Debian, Kali, Mint etc. This mechanism is named as for each some programming languages where a list is iterated over.

Syntax

Syntax offor look is like below.

  • for is the keyword which is used to create loops in bash
  • F is the element or item which is populated in each step from ITEMS
  • ITEM1, ITEM2, ITEM3, … are items we want to iterate this can be also some list which contains multiple items
  • CODE is the implementation part which is executed in each loop

Numeric Syntax

One of the most used Numeric syntax. We will provide numbers as a list and iterate over given list.

In numeric syntax, it is very similar to the general syntax where we provide a collection of numbers.

Given File List Syntax

We will provide the files as a list and use them in each iteration. In this type, we will iterate over the list of files like file1, file2, file3 etc.

LEARN MORE  Linux usermod Command Tutorial With Examples

Command Output Syntax

We can use bash commands output as items for iterate. In this syntax, we expect that the $(BASH_COMMAND) will return a list where we will iterate over this list.

Loop Over Given File Names

The simplest usage for for loop is over given file names. We will provide the file files by separating them with spaces. In this example, we will provide file names a , b and c and then print them with some string.

Loop Over Given File Names

Loop Over Given File Names

Loop Over Listed File Names

What can we do if there are thousands of files to be a loop in a directory. We need a more dynamic way to provide file names. We can use ls command in order to provide file names as a list without typing one by one.

Loop Over Listed File Names

Loop Over Listed File Names

Loop Over Specified File Extensions

Some times we may need to work on specific file extensions. We can specify the file extension we want to loop with for loop. In this example, we will print encoding types of files with *.txt extension.

LEARN MORE  How To Split, Create and Close Tmux Screen In Linux?

Loop Over Specified File Extensions

Loop Over Specified File Extensions

Loop Over Files Reading From Text File

Files names can be stored in a text file line by line. We can read file names from the specified text file and use in a for loop. In this example, we will read the following text file and loop over lines. Our file name is filenames.txt

Loop Over Files Reading From Text File

Loop Over Files Reading From Text File

C Like For Loop

Up to now, we have learned the basic syntax of for loop. There is also more formal for loop which is the same as C Programming language. We need to provide start condition, iterate operation and end condition.

In this example, we will use echo command to print from 1 to 5 with this for loop syntax.

C Like For Loop

C Like For Loop

Infinite Loop

In some cases, we may need infinite loops. The infinite loop will never end except its process is killed. We will use C like for loop in order to create an infinite loop

Infinite Loop

Infinite Loop

Conditional Break with exit

During for loop, we may need to exit for given conditions if they occur. exit keyword can be used to break the iteration. In this example, we will check and if the current value of c can be divided into 6 we will end the loop.

LEARN MORE  How To Make Directory in Python?

The terminal will be closed when the variable $c is equal to the 6 where the if will be true and exit statement will be executed.

Skip To Next Step with continue

In some cases, we may need to skip the current iteration but resume to the loop. We can use continue keyword which will step over to the next iteration. In this example, we will continue if the $c variable can be divided with 3.

Skip To Next Step with continue

Skip To Next Step with continue

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)