What is the best way, using Bash, to rename files in the form:
(foo1, foo2, ..., foo1300, ..., fooN)
With zero-padded file names:
(foo00001, foo00002, ..., foo01300, ..., fooN)
What is the best way, using Bash, to rename files in the form:
(foo1, foo2, ..., foo1300, ..., fooN)
With zero-padded file names:
(foo00001, foo00002, ..., foo01300, ..., fooN)
The following will do it:
EDIT: changed to use ((i=1,...)), thanks mweerden!
To left-pad numbers in filenames:
Explanation
`echo ... $2}\`
(The backslash, \, immediately above just splits that one-liner over two lines for readability)[0-9]*.[a-z]*
$f
) to pass it toawk
-F.
:awk
field separator, a period (.
): if matched, separates the file names as two fields ($1
= number;$2
= extension)printf
: print first field ($1
, the number part) as 4 digits (%04d
), then print the period, then print the second field ($2
: the extension) as a string (%s
). All of that is assigned to the$tmp
variable$f
) to the new filename ($tmp
)Here's a quick solution that assumes a fixed length prefix (your "foo") and fixed length padding. If you need more flexibility, maybe this will at least be a helpful starting point.