There are many ways to left pad numbers in Linux. Usually this seems to be done by adding leading zeros in front of the main number. A few examples of doing this can be seen below:

Using printf

# left pad with zeros
printf "%05d\n" 99
> 00099

# left pad with zeros and other text
printf "my-file-name-%05d.txt\n" 99
> my-file-name-00099.txt

Using AWK

# left pad with zeros using awk
echo 99 | awk '{printf "%05d\n", $0}'
> 00099

Looping in Bash

# left pad with zeros bash loop
for i in {00001..00005}; do
  echo $i
done

> 00001
> 00002
> 00003
> 00004
> 00005

Leave a Reply

How to Left Pad Numbers in Linux