linux
Exploring 'for' loop in bash
Filed in archive Tips & Tricks by mateusz on October 23, 2007
Exploring 'for' loop in bash
Hello, my name is Mateusz and I am one of the IT guys behind Creative Weblogging. I will start a new mini-series here on this blog writing once in a while about interesting "hacks" or "tips&tricks" I have to use, discover or come up with every day at work. I hope it will be interesting and valuable to see how to make your life much easier when instead of hours of manual labor you can just type couple of lines in Shell and it will do the work for you.
Let's kick off this series with an introduction to a for loop. It's a simple and well-known concept, yet many people don't even realize it's available right there in front of you in every bash.

For loop in bash has many flavors, but that's a topic for another article, let's focus today on one of the simplest ones:

for name in *jpg
do
        echo $name;
done


As you can imagine, this will list all the jpg files in my current dir. No better than ls. But wait, how about doing something with each and every of those files?

for name in *jpg
do
        echo $name;
        convert -resize 100x $name $name;
done


(For the above to work you need to have imagemagick installed)

With those couple of lines we have just resized all the pictures in current dir to thumbnail size. Be careful, we write back to the same file (as the second parameter to convert shows), so there is no way back.

As you can imagine from this simple example there is huge potential there. We will explore more of the for loop in further articles.


Permalink: Exploring 'for' loop in bash
Tags: linux  2007  bash  loop  loop+bash  exploring+loop  tips+tricks 
Trackback: http://publish.creative-weblogging.com/publish/mt-tb.pl/98420
img Addthis img Ask img Blinklist img del.icio.us img Digg img Fark img Facebook img Google img Lycos img Ma.gnolia Add this page to Mister Wong Mr Wong img Netscape img Netvousz img Newsvine img Reddit img StumbleUpon img Slashdot img Tailrank img Technorati img Wink img Yahoo

Vote for Exploring 'for' loop in bash:

  • Currently 8.50/10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
Rating: 8.50 out of 2 vote(s) cast.
 
Subscribe
Share It
RSSrss
See all blog subscribe options
Google google
What is RSS?
Yahoo! yahoo
Addthis Subscribe using any feed reader!
Bloglines Bloglines
Newsletter

TwitterFollow us on Twitter!