How To Linux Bash For Loop In Files?

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-loop-files/
PINTEREST
PINTEREST
INSTAGRAM

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 uses for loop to iterate over files and folder. In this tutorial we will look how to use for loop to iterate over files and directories in Linux. This examples 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.

Here for will loop over ITEM1, ITEM2, etc. and run CODE in every step and use F .

Numeric Syntax

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

Given File List Syntax

We will provide the files as list and use them in each iteration.

Command Output Syntax

We  can use bash commands output as items for iterate.

LEARN MORE  Linux wc Command Word and Line Count Tutorial With Examples

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 is thousand of files to be loop in a directory. We need 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.

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 specified text file and use in a for loop. In this example we will read 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 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.

LEARN MORE  Enumerate() In Python

C Like For Loop
C Like For Loop

Infinite Loop

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

Conditional Break with exit

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

Continuation with continue

In some cases we may need to skip 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 6 .

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-loop-files/
PINTEREST
PINTEREST
INSTAGRAM