pouët.net

Hey regex gurus

category: code [glöplog]
 
I need a way to batch-rename a BUNCH of Amiga-style MOD.* to everything-else-style *.mod . Blasphemy, I know!

I have the usual Linux/OSX tools at my disposal (perl, sed, bash, find -exec, etc.)

Does not have to be recursive but the option would be nice.

Any pointers?
added on the 2015-01-28 02:18:01 by jmph jmph
Code:rename -v -n 's/\.MOD$/.mod/' *.MOD


and then remove -n if the results look good
added on the 2015-01-28 02:24:40 by Gargaj Gargaj
oh wait misread. this instead:
Code:rename -v -n 's/MOD\.(.*)$/$1.mod/' MOD.*
added on the 2015-01-28 02:27:19 by Gargaj Gargaj
Perfect, thanks Gargaj!
added on the 2015-01-28 02:32:54 by jmph jmph
That rename seems like a useful bit. The generic oldskool way would be something like:

Code:for n in MOD.* ; do mv "$n" "`echo "$n"|sed s/MOD\.//`.mod" ; done
added on the 2015-01-28 07:48:39 by Marq Marq
Actually the most accurate way would be
Code:rename -v -n 's/^MOD\.(.*)/$1.mod/' MOD.*
added on the 2015-01-28 15:15:18 by Gargaj Gargaj
I quite like this website to try out regex: https://regex101.com/
added on the 2015-01-28 16:11:57 by Rob Rob
for the record, this should also work on any POSIX shell:
Code:for n in MOD.*; do mv "$n" "${n#*.}.mod"; done
added on the 2015-01-28 17:37:09 by reed reed
Also for the record: There are bulk renaming tools with proper UIs and such. ;)

(I only need ren every couple of weeks)
added on the 2015-01-28 18:16:57 by tomaes tomaes
Let's do the subdirectories too, while we're at it:
Code:find -iname "mod.*" -print0 | xargs -0 -I {} rename -v -n 's/mod\.(.*)$/$1.mod/i' {}

Might blow up on your face under some conditions, who knows :)
added on the 2015-01-28 18:37:05 by Marq Marq

login