Padding numbers in filenames
Chris F.A. Johnson
cfaj-uVmiyxGBW52XDw4h08c5KA at public.gmane.org
Mon Feb 26 21:38:57 UTC 2007
On Mon, 26 Feb 2007, William O'Higgins Witteman 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.
There are at least two different versions of rename, and they do
not take the same syntax.
A shell script (this is bash/ksh93 specific) is straightforward:
for file in [1-9]*.png
do
newname=0000$file
newname=${newname: -8} ## POSIX: newname=${newname#"${newname%????????}"}
mv "$file" "$newname"
done
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
===================================================================
Author:
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
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