bash: Looping over a range

In: Computers|Linux Tips|Rants & Raves

14 Apr 2009

This falls into the category of things that are simple yet I’m always trying to remember. Sometimes in bash I find myself wanting a for loop, not of the usual foreach type (for i in `ls /etc/`; do ...), but a more classical one (a la C-style for var=0; var<100; var++ ... syntax). Here’s the bash way:


for i in {0..99}; do
echo $i
done

The output is a list of numbers from 0 to 99. For a fun example of when you might need this, consider the case of noticing that Postfix isn’t running on a machine and starting it. Your mail client promptly crashes, and the flurry of output in the log indicates that it’s delivering mail as fast as it can, but complaining that some mail has been in the queue for 180 days. “Holy smokes,” you exclaim, stopping Postfix. “Just how much mail is there?” You’re not entirely sure because you aborted your ls after it sat for a good minute with no output. That’s never good. du -sh shows about a gig and a half.

“I’ve got this under control,” you think. rm -f /var/spool/postfix/*. Ta-da! Wait, what’s this “/bin/rm: Argument list too long” business? That error can’t possibly be good. rm very rarely complains. So we tried a smaller delete size, thinking we could do postfix/1*, postfix/2*, etc. Just step through it. That, too, was too much for rm.

So it ended up taking an absurd for i in {0..99}; do rm -f /var/spool/postfix/$i; done to purge the mail. (And that didn’t catch all of it, either; I’m not sure off-hand how to do this in hex.) Each iteration deleted somewhere around 2,000 mail messages, making me think there were something like a half-million messages. (00-FF, times a little under 2,000.)

Related posts:

  1. Viewing all cron jobs
  2. Reclaiming Your Inbox
  3. Spam
  4. High Dynamic Range
  5. Fun with Shell Commands

Comment Form

On Other Sites

  • i5junkies: Thanks for this posting. It helped me fix the problem and running. GREAT post!!! [...]
  • emeka: i accidentally unscred all the screws and flipped my laptop now i dont know which screw goes where p [...]
  • Erik: Hey Matt! I came across your blog after seeing you had posted a link to our older website. I know t [...]
  • mot: haha - thats all i could say... thanks [...]
  • Motorola Phones - The RadioReference.com Forums: [...] Have a look at this page: Direct Talk Compatible Phones | Matt's Blog [...] [...]