I have multiple files Batch1_00000001
till Batch9999_00000001
containing the following filenames:
Batch1_00000001.pdf
Batch10_00000001.pdf
Batch100_00000001.pdf
Batch1000_00000001.pdf
First of all I want to remove the word Batch
and add the letter b
to the front of the filenames. After that I want to add up to three leading zero's at the start. So that b1_00000001.pdf
becomes b0001_00000001.pdf
like:
b0001_00000001.pdf
b0010_00000001.pdf
b0100_00000001.pdf
b1000_00000001.pdf
Does anyone have a solution to break it all down, or make a single regex rename
solution? Thanks in advance!
You can try:
#!/bin/bash
cd /directory_contain_files
for file in *.pdf; do
index=$(echo $file | cut -d'_' -f1 | grep -o "[0-9]*")
if [[ $index -lt 10 ]]; then
index="000$index"
elif [[ $index -lt 100 ]]; then
index="00$index"
elif [[ $index -lt 1000 ]]; then
index="0$index"
fi
mv $file b${index}_00000001.pdf
done