[ Content | View menu ]

Padding Numbers in Filenames

Written on April 8, 2008

I sometimes find myself with scads of files in a directory that, due to the vagaries of sort() do not show up in the “right” order. For example, 1.png, 2.png, … 751.png.

I was poking around with rename, but I wasn’t finding a good way to do this. Thankfully, a poster on TLUG showed me how to use sprintf to get the results I wanted, like so:

rename -n 's/(\d+)/sprintf("%04d", $1)/e' *.png

Very neat.

Filed in: blog,code.

Comments closed