Adding zero padding to file name with bash

wz-billings :

I have a directory full of folders named, e.g., "Week1", "Week2", ..., "Week9", and now I have come to the point where I need a Week10. Having not anticipated this problem beforehand, I now need to rename all of my directories to be "Week01", "Week02", ..., "Week09".

Normally I would do this by hand but I know enough about the MacOS bash terminal to know that I should be able to accomplish this with something like

for f = Week[1-9]; do
   mv $f something something
done

But I don't know what to put in the "something something" to change the names how I want. How can I insert the zeros into my file names?

Carl Norum :

Use printf(1)!

for OLD in {1..9}
do
    NEW=$(printf %02d ${OLD})
    mv Week${OLD} Week${NEW}
done

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=390828&siteId=1