Search code examples
bashrename

How to zero pad numbers in file names in Bash?


What is the best way, using Bash, to rename files in the form:

(foo1, foo2, ..., foo1300, ..., fooN)

With zero-padded file names:

(foo00001, foo00002, ..., foo01300, ..., fooN)

Solution

  • In case N is not a priori fixed:

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