bashshellleading-zero

Bash script for adding two leading zeros in the middle of the file names


I have the files with two numbers in the middle of each file:

FileName_46_01.wav
FileName_6_15.wav
FileName_3_7.wav
FileName_15_22.wav

and so on.

I need to rename files with shell script to add the leading zeros if it's needed:

FileName_46_01.wav
FileName_06_15.wav
FileName_03_07.wav
FileName_15_22.wav

Solution

  • Like this, using Perl's rename command:

    rename -n 's/\d+/sprintf "%.2d", $&/ge' ./*.wav
    

    Remove -n if happy with the output (dry-run mode).