Zero Padding in Bash

This is as much a reminder for myself as anything. I needed to create 31 folders named 00 through 31 for each day of the month in a web-directory used to store audio files from CAPE-1 transmissions. The first thought that came to mind was to write a quick one-liner bash command/script to create those folders. So, without much thought, I quickly typed the following into a shell on my web server:

for ((x=1;x<=31;x+=1)); do mkdir $x; done

OK, well that did create 31 directories, but the first 9 were named 1, 2, 3, etc. instead of 01, 02, 03 like I wanted. After a quick search on zero-padding in bash it seemed no one had posted anything about it except to say it was tricky. Being a C programmer before a bash script writer, I thought to myself, “Wouldn’t it be nice if I could use a printf() statement in bash?” Then I remembered that you can, only usually it is used for formatting user interface statements. I could not think of any reason it would not work to deliver the argument to a mkdir statement, so I tried:


$ for ((x=1;x< =31;x+=1)); do mkdir `printf "%02d" $x`; done
$ ls
01 03 05 07 09 11 13 15 17 19 21 23 25 27 29 31
02 04 06 08 10 12 14 16 18 20 22 24 26 28 30

And it works! Breaking the command down gives a standard bash for loop with the mkdir command as the internal command inside the loop. As an argument to the mkdir command, however, is a printf statement which given the formatting prints a zero-padded (the 0 after %) two-character long (the 2 after the 0) integer, x. Remember, the `s are back-ticks (below the tilde on most keyboards), not single quotation marks (’).

It seems like there should be a better way of doing this, but quickest way I found to add zero padding to a number in bash is to use the provided printf which uses the same formatting as the C language printf statement. More usage and examples are available at the ss64.org bash printf reference page.

14 Responses to “Zero Padding in Bash”


  1. 1 gabe

    Man, this is great!
    You have no idea how often it annoyed me that bash can’t do zero-padding, but I didn’t know about the bash’s printf command. Thanks a lot, this really made my day! I need to number thousands of files (output from a scientific simulation) and then concatenate them later in the right order, so I really need zero-padded numbers in the filename. I found your blog-entry with Google’s “I’m feeling lucky” and the searchwords “zero padded integers BASH”…
    Maybe there should be an easier way to do this in bash, but it works pretty well for me this way.
    Thanks again for posting this, you’ve helped out scientific progress ;-)

  2. 2 Jonathan

    gabe,

    Glad you found it helpful. It does seem that there should be a cleaner way, but this was the best I found.

  3. 3 Nikhil

    Thanks! It is true that searching the web doesn’t give much help for padding numbers in bash! Your article was really helpful!

  4. 4 John

    For generating this sort of thing seq can generate zero padded sequences of numbers…

    for i in `seq -f ‘%02g’ 1 31` ; do mkdir $i ; done

  5. 5 al

    Starting to work on some BASH stuff the other day and this saved my sanity. Thanks!

  6. 6 David

    seq has an option to zero-pad (-w) so you can just do this:

    seq -w 1 31

  7. 7 snakehsu

    Thanks for the tips. For your particular case you can use seq no problem. But sometimes you may need to use these numbers as array index, which is my case, in which I cannot use seq -w as Bash consider 01, 02 … as octet numbers so 08 and 09 would be “illegal” index. So I used seq without -w as index and used your printf method to generate zero paddings when I need them in the filename.

  8. 8 William Tracy

    Thank-you.

    Thank-you, thank-you.

  9. 9 pedro

    Great stuff, I used to program in C and C++ but never really realised how printf() could fit in shell commands/scripts.

    Now I do! Good on ya, thanks

  10. 10 SirPavlova

    For anyone wanting to do this using jot, try “jot -w %03d 10 1″ for 001 to 010, etc. Hopefully this saves one or two people a trip to the man pages.

  11. 11 gxx

    Thanks SirPavlova, that was exactly what I was looking for…

  12. 12 Binny V A

    Thanks! I was going to write a script for this - you saved my time.

  13. 13 mardson

    thank you all. much time saved, much work accomplished, minimal effort expended ;)

  14. 14 Thul Dai

    Thanks a lot, very useful.

    What I often do as a workaround is add 100 (or 1000 as it were) so my ensemble runs (and respective folders, files, …) are numbered from 101 to 150 instead of 1 to 50. That works also easily in Fortran and other funky languages.

    TD

Leave a Reply