[ale] Generating sequential files with leading zeros from bash

Charles Shapiro hooterpincher at gmail.com
Fri Apr 6 15:45:37 EDT 2007


Look up printf(1).
e.g.

"printf "%5.5d\n" 40
00040

Looks like "*" works too, at least in bash(1):

printf "%*.*d\n" 7 7 40
0000040

-- CHS


On 4/6/07, Greg Freemyer <greg.freemyer at gmail.com> wrote:
>
> 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.
>
> Thanks
> Greg
> --
> Greg Freemyer
> The Norcross Group
> Forensics for the 21st Century
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
>
-------------- next part --------------
An HTML attachment was scrubbed...




More information about the Ale mailing list