This is a gem I use a lot, and forget a lot, so it's write it down time.
Suppose I have a folder with a number of tarballs in it and I want to extract all of them, tar -xf *.tar won't work as tar will try and extract all but the first file that matches the wild card from the first item. We break out the trusty for loop.
for foo in `ls *.tar`
do
tar -xf $foo
done
The above works at when entered line by line into the prompt or as part of a script. And to be even kinder, it appears in your history as for foo in `ls` ; do echo $foo; done
And yes, I was just extracting the drupal 6 versions of eveyr module I use.
And yes, I should be ashamed of myself for needing to look up a for loop in bash. I guess I'm prone to breaking out python, haskell or the dreaded perl as soon as I realise I'm going to have to write more than one line.





Add new comment