Tuesday, October 25, 2011

Renaming file numbers

This little script will rename badly named files into nice 3 digit numbers.

For example, if you have files, 1,2,3,...10,11,12, it will rename the files into 01, 02, 03, etc.

#!/bin/bash
echo $1;
mkdir $1/output

for each in `ls $1`
do
filename=$(echo $each | cut -f1 -d '.' | tr -d '[[:digit:]]')
extension=$(echo $each | cut -f2 -d'.')
number=$(echo $each | tr -cd '[[:digit:]]')
echo $number;
cp $1/$each $1/output/$filename`printf "%03d" $((10#$number))`.$extension
done

No comments:

Post a Comment