[ale] Generating sequential files with leading zeros from bash

Cox, Danny dcox at icc.net
Fri Apr 6 15:46:24 EDT 2007


Greg,

-----Original Message-----
From: ale-bounces at ale.org [mailto:ale-bounces at ale.org] On Behalf Of Greg
To: ale at ale.org
Freemyer
Sent: Friday, April 06, 2007 3:38 PM
To: Atlanta Linux Enthusiasts
Subject: [ale] Generating sequential files with leading zeros from bash

All,

I need to do a rename on a bunch of sequential files from something
like:

output-000001.pgm
output-000002.pgm
output-000003.pgm
...

to a new name that has a different number of leading zeros and a
different starting number.

I now how to manage the counter, but how can I create a filename with
leading zeros from a bash script and a counter variable.

ie. in C code I would do sprintf(dest_filename, "prefix-%07d.pgm",
counter)

Is there something similar to sprintf in bash?

And to make the issue even more difficult, I would like the "07"
portion of the above to itself be variable.  ie. Sometimes I will want
7 digits, other times 8, etc.

See "printf" the command.  It should do what you want.

Danny




More information about the Ale mailing list