Bash loops for productivity

Working in bash is so much fun! If you spend enough time in terminal, then you might get addicted to it and never like the gui windows. There are commands (especially loops) that can save you lot of time. They are very useful to do some routine stuff. My favorite loops are as follows:
Loops through all the files with txt extension and performs the action

for f in *.txt; do yourcommand $f >$f.out; done

Read file line by line and run command on it

while read line; do yourcommand $line; done<FileToRead.txt

Other variation of this above command (extremely useful when you have to read arguments from a file:

while read fld1 fld2 fld3; do YourCommand -a $fld1 -b $fld2 -c $fld3 > $fld1.$fld2.$fld3.txt; done<FileToRead.txt

A simple loop for a set of numbers (you can also use {a..z} etc., or mix them)

for i in {1..10}; do echo $i; done

Another variation of the above command

for i in {1..22} X Y; do echo "human chromosome $i"; done

I hope these will help you too!

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s