Friday, June 24, 2011

basename and dirname in UNIXes

I was reading The Unix For Loop today and I found that the following code will fail in the case of a filename like anoldfilename.old:

for i in *.old 
j=`echo $i|sed 's/old/bak/'` 
mv $i $j 

I think the following will solve that problem:

j=`basename $i .old`
p=`dirname $i`

mv $i $p/$j.bak

The right tool for the right job!

No comments: