Renaming files to put a leading zero in them (Linux)

A cheap MP3 player had some issues with an audio book with more than 100 files. The files had a single leading zero prefix as until recently there were no books on it with more than 100 files. The below code block renames the files.

OIFS="$IFS"
IFS=$'\n'
for file in `find . -type f -name "*.m4a"`  
do
echo "file: = $file" ;
mv "$file" "$(echo ${file} | sed -r 's/\b([0-9]{2}[\.])/0\1/')";
done
IFS="$OIFS"

Comments

Leave a Reply