Linux Shell Script to Add Leading Zeros to File Names

Linux shell script to add leading zeros to file names

Try:

for a in [0-9]*.txt; do
mv $a `printf %04d.%s ${a%.*} ${a##*.}`
done

Change the filename pattern ([0-9]*.txt) as necessary.


A general-purpose enumerated rename that makes no assumptions about the initial set of filenames:

X=1;
for i in *.txt; do
mv $i $(printf %04d.%s ${X%.*} ${i##*.})
let X="$X+1"
done

On the same topic:

  • Bash script to pad file names
  • Extract filename and extension in bash

Add leading zeros to integer section of filename

So, after looking through the answers submitted here and elsewhere on SO this is what I ame up with:

find . -name '*\-[0-9][0-9].tif' -exec sh -c '
for f do
mv "$f" "${f//\-/\-0}";
echo "$f"
done' _ {} +

This works on files with two digits in the integer section, bringing them up to 3 digits. For single digit files I alter slightly and run again.

One nice thing about this script is that it works on subfolders.

I do have to admit to not understanding it completely. I have no real idea why doneis followed by ' _ {} + . I guess that's the next thing I'll have to look up :-).

bash shell: change multiple file names to add leading zeros


for file in prefix.[0-9].*
do
mv "$file" "${file/prefix./prefix.0}"
done

Add leading zeros twice in filename

With perl based rename command

$ touch 142_27.jpg 7_39.jpg 1_120.jpg
$ rename -n 's/\d+/sprintf "%03d", $&/ge' *.jpg
rename(1_120.jpg, 001_120.jpg)
rename(142_27.jpg, 142_027.jpg)
rename(7_39.jpg, 007_039.jpg)

The -n option is for dry run, remove it for actual renaming



If perl based rename command is not available:

$ for f in *.jpg; do echo mv "$f" "$(echo "$f" | perl -pe 's/\d+/sprintf "%03d", $&/ge')"; done
mv 1_120.jpg 001_120.jpg
mv 142_27.jpg 142_027.jpg
mv 7_39.jpg 007_039.jpg

Change echo mv to just mv once dry run seems okay

How to add leading zero's to sequential file names

You could use something like this:

files="*.jpg"
regex="(.*-)(.*)(\.jpg)"
for f in $files
do
if [[ "$f" =~ $regex ]]
then
number=`printf %03d ${BASH_REMATCH[2]}`
name="${BASH_REMATCH[1]}${number}${BASH_REMATCH[3]}"
mv "$f" "${name}"
fi
done

Put that in a script, like rename.sh and run that in the folder where you want to covert the files. Modify as necessary...

Shamelessly ripped from here:

Capturing Groups From a Grep RegEx


and here:

How to Add Leading Zeros to Sequential File Names

How to zero pad numbers in file names in Bash?

In case N is not a priori fixed:

for f in foo[0-9]*; do
mv "$f" "$(printf 'foo%05d' "${f#foo}")"
done

How to pad file name with leading zeroes using bash

Try doing this in a shell :

using bash :

$ a=62_02.png
$ printf -v newvar %04d_%s ${a%_*} ${a##*_}
echo "$newvar"
0062_02.png

Another concise solution :

$ ( a=62_02.png; IFS=_; printf '%04d_%s\n' $a )
0062_02.png

How to add leading zeros for for-loop in shell?

Use the following syntax:

$ for i in {01..05}; do echo "$i"; done
01
02
03
04
05

Disclaimer: Leading zeros only work in >=bash-4.

If you want to use printf, nothing prevents you from putting its result in a variable for further use:

$ foo=$(printf "%02d" 5)
$ echo "${foo}"
05


Related Topics



Leave a reply



Submit