Thursday, January 25, 2007

File renaming in BASH via string handling

#!/bin/bash

#remove spaces
for i in *.wma; do
mv "$i" `echo $i | tr ' ' '_'`;
done

#remove uppercase
for i in *.[Ww][Mm][Aa]; do
mv "$i" `echo $i | tr '[A-Z]' '[a-z]'`;
done

#convert file names
for i in *.wma; do
mv "$i" "`basename "$i" .wma`.mp3";
done

#rename test1a, test2a, test3a
#to t1a, t2a, t3a
for i in test*a; do
substring1=${i:0:1}
substring2=${i:4:1}
mv $i $substring1$substring2'a'
done;

No comments: