2

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" 
    fi 
done
Cazz
  • 145

1 Answers1

5

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}"; 
done

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.

Terrance
  • 41,612
  • 7
  • 124
  • 183
steeldriver
  • 136,215
  • 21
  • 243
  • 336
  • 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 '19 at 19:22
  • @Cazz apologies that was a dumb error on my part - please try it now – steeldriver Jul 27 '19 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 '19 at 21:07