With big help from here, I have soon converted all my jpg files to gif (thanks again ALOT)

but when I was looking around at my files I have noticed that some folder I have added a zero to the filenames.

The filename standard I trying to have is 001.gif, 002.gif.... but some of the files have 01.gif, 02.gif and so on and that is not good for me.

So with the code, I got from convert that I find something that calls globe star I was ready to see if I can fix that for myself :)

but nothing happened when I run the code. Right now I just try to see if I can convert the file that has JPG

for f in /lib/**/*jpg; do
        if [ "${#f}" -eq 2 ]; then
    mv "$f" "0$f" 

1 Answer 1


There are lots of different ways to do this, but taking your attempt as a starting point

for f in /lib/**/[0-9][0-9].jpg; do
  b="${f##*/}"; d="${f%/*}"; 
  echo mv "$f" "${d}/0${b}"; 

That is, instead of matching all jpg files and then trying to test how many digits each has, just match the ones with exactly two leading digits. Remove the echo once you are satisfied that it's working correctly.

Alternatively you could something like this with the Perl-based prename command:

prename -n 's/(\d+)\./sprintf("%03d.",$1)/e' /lib/**/*.jpg

This takes any non-empty sequence of digits before a . and reprints them zero-padded to a minimum width of 3 digits.

  • Hmm very simple idea and I love it But I have problem with the code, it add 0 at the beginner of the address and not the filename.
    – Cazz
    Jul 27, 2019 at 19:22
  • @Cazz apologies that was a dumb error on my part - please try it now Jul 27, 2019 at 20:05
  • ohh no I have to thank you. it works fine and now I just have to understand why it works and I think I know why.
    – Cazz
    Jul 27, 2019 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.