[kwlug-disc] bulk renaming with regex

Steve Izma sizma at golden.net
Wed Sep 28 22:02:17 EDT 2022


On Wed, Sep 28, 2022 at 09:34:52PM -0400, Doug Moen wrote:
> Subject: Re: [kwlug-disc] bulk renaming with regex
> 
> For bulk renaming, instead of using a bulk renaming tool with a
> limited pattern language, I put all of the filenames into a
> text file, one filename per line, then I use a sequence of vim
> substitute commands to convert each filename into a shell
> command that uses 'mv' to rename the file. Then I execute the
> file as a shell script....

That's what I do frequently, for example to rename the images
from a camera into filenames that are more helpful than the
numbering system used by various cameras.

But in this case, if it's just a simple change from s01enn to
s02enn, I'd just use a shell "for" loop:

for fn in s01*; do out=s02${fn##s01}; mv $fn $out; done

I use the korn shell, but I think bash is pretty similar. The ${}
structure also allows for more sophisticated substitutions. See
"Parameter Expansion" in bash(1), especially:
${parameter/pattern/string}

	-- Steve

-- 
Steve Izma
-
Home: 35 Locust St., Kitchener, Ontario, Canada  N2H 1W6
E-mail: sizma at golden.net  phone: 519-745-1313
cell (text only; not frequently checked): 519-998-2684

==
The most erroneous stories are those we think we know best – and
therefore never scrutinize or question.
    -- Stephen Jay Gould, *Full House: The Spread of Excellence
       from Plato to Darwin*, 1996




More information about the kwlug-disc mailing list