How do I add zero padding to filenames that already have numbers in them? [duplicate]

I'm assuming that you just need to rename the files 1-9, since those are the ones that need padding. There are multiple ways of doing this.

You can execute the below command:

for n in $(seq 9); do mv track_$n.mp3 track_0$n.mp3; done;

This will rename tracks track_1.mp3 - track_9.mp3 to track_01.mp3 - track_09.mp3.


To break it down, it looks like this:

for n in $(seq 9)
do
    mv track_$n.mp3 track_0$n.mp3
done
  • for n in $(seq 9): for every number in the output of the command seq 9, which is a command that just lists numbers 1 to 9, do,
  • mv track_$n.mp3 track_0$n.mp3: this is the actual command that renames the files. It substitutes the value of n iterating through all numbers. So it does mv track_1.mp3 track_01.mp3, mv track_2.mp3 track_02.mp3, until that last number which is 9.

The script below wil rename files is given directory. It calculates the number of leading zeros needed, no matter the number of files (if >100, more zeros are needed), and renames the files automatically.

To use it

copy the script below into an empty file, in the headsection, set the sourcedirectory, the prefix ("track-" in this case) and the file extension of the files you want to rename. Save it as rename.py and run it by the command:

python3 /path/to/script.py

The script:

#!/usr/bin/env python3

import shutil
import os

sourcedir = "/path/to/sourcedir"
prefix = "track_"
extension = "mp3"

files = [(f, f[f.rfind("."):], f[:f.rfind(".")].replace(prefix, "")) for f in os.listdir(sourcedir) if f.endswith(extension)]
maxlen = len(max([f[2] for f in files], key = len))

for item in files:
    zeros = maxlen - len(item[2])
    shutil.move(sourcedir+"/"+item[0], sourcedir+"/"+prefix+str(zeros*"0"+item[2])+item[1])

Of course there are many ways of doing it. You can, for instance:

  • Separate the parts
  • pad the part that has the number
  • concatenate the individual part back to the new name

Put this into a loop:

#!/bin/bash

item="track_1.mp3"

part1=`echo $item | awk -F_ '{print $1}'`
part2=`echo $item | awk -F_ '{print $2}'`
part2a=`echo $part2 | awk -F. '{print $1}'`
part2b=`echo $part2 | awk -F. '{print $2}'`
number=`printf "%02d" $part2a`
seperator="_"
newname="$part1$seperator$number.$part2b"
echo $newname

The loop:

#!/bin/bash

for item in track_1.mp3 track_10.mp3 track_11.mp3
do
    part1=`echo $item | awk -F_ '{print $1}'`
    part2=`echo $item | awk -F_ '{print $2}'`
    part2a=`echo $part2 | awk -F. '{print $1}'`
    part2b=`echo $part2 | awk -F. '{print $2}'`
    number=`printf "%02d" $part2a`
    seperator="_"
    newname="$part1$seperator$number.$part2b"
    echo "Renaming $item -> $newname"
    mv $item $newname
done
enter code here

GPRename will automatically insert a zero if you are renaming 10 or more files (two if you are renaming 100 or more, etc.) when using the numerical function. Just make sure Zero auto-fill is turned on in the Options menu.