I've recently discovered the power of sed; regular expressions seem like they'll take a lifetime to master. This tiny script uses both to add a zero before the single digit in file names.
Can this be done more easily without sed?
Is the regex sound?
#!/bin/bash
# > addzero.sh <
# Adds a zero before the single digit in file names separated by an underscore.
# Designed to help keep files in sequence in directory listings. Otherwise, you get listings like
# "_0.txt, _1.txt, _10.txt, _11.txt, _12.txt, ..., _2.txt, _20.txt, _21.txt, _22.txt, ..."
for filename in $(ls -1 ./*); do
if [[ $filename =~ _[0-9]\. ]]; then
new_filename=$(echo $filename | sed -n 's/_\([0-9]\)\./_0\1\./p')
mv $filename $new_filename
fi
done