Padding numbers in filenames

Christopher Browne cbbrowne-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Mon Feb 26 16:53:15 UTC 2007


On 2/26/07, William O'Higgins Witteman <william.ohiggins-H217xnMUJC0sA/PxXw9srA at public.gmane.org> wrote:
> I have a mess of sequentially numbered files (1.png, 2.png ... 824.png)
> that I'd like to pad the filenames of so that they sort prettily
> (0001.png, 0002.png ... 0824.png).  I thought to use the rename command,
> but I'm not getting the results I need, and Google didn't help.
>
> Here's what I tried with rename:
>
> rename -n "s/(\d)\.png/000$1png/" *
>
> This would only catch the first 9 files, but I no that I had the right
> method.
>
> I can certainly whip up a quick program in Python or Perl (I could
> probably learn enough Ruby in half an hour too, or Lua, or C, or ...
> you get the picture) but I was curious where I was going wrong with
> rename.  Any suggestions?  Thanks.

Well, here's something that echos the commands to the command line:

cbbrowne at dba2:~/> for i in `seq 12`; do
f1=`printf "%d.png" $i`
f2=`printf "%03d.png" $i`
echo "mv $f1 $f2"
done
mv 1.png 001.png
mv 2.png 002.png
mv 3.png 003.png
mv 4.png 004.png
mv 5.png 005.png
mv 6.png 006.png
mv 7.png 007.png
mv 8.png 008.png
mv 9.png 009.png
mv 10.png 010.png
mv 11.png 011.png
mv 12.png 012.png

Change the last line as shown below to get the renaming to actually
take place...
for i in `seq 12`; do
f1=`printf "%d.png" $i`
f2=`printf "%03d.png" $i`
mv $f1 $f2
done
-- 
http://linuxfinances.info/info/linuxdistributions.html
"...  memory leaks  are  quite acceptable  in  many applications  ..."
(Bjarne Stroustrup, The Design and Evolution of C++, page 220)
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list