swestrup: (Default)
[personal profile] swestrup
Am I right that there isn't a half-decent bulk rename command out there for Linux?  I did a search and found a bunch, but they all rely on regex which is not terribly useful when what you want to do is pull a number out of the middle of a file name, do math on it, and depending on the result replace it with a string containing a different embedded number.

(Okay, maybe this can be done with Perls ultra-extended regular expressions, but after rereading how they work a dozen times, and trying 30 different patterns, I gave up as the documentation is inadequate and I haven't been able to figure out how to express this.)

This, by-the-way is what you want to do if you have 1000 files numbered sequentially, from 10 years of a TV show, and you want to rename them as season and episode numbers, and the number of episodes per season was not a constant...

Okay, I know this is an edge case, but if I were to write a bulk rename, this would be one of the things I would, for certain, want to make easy.  Anything less complex and you can just throw together a one-line bash for-loop to handle it.

Date: 2009-02-14 08:03 am (UTC)
From: [identity profile] sps.livejournal.com
I'd use Emacs to do it quasi-interactively, or Perl—there's a fully functional rename, it wouldn't have to be a regexp at all. Or am I missing something?

Just how hairy is the mapping you need?

Date: 2009-02-14 08:27 am (UTC)
From: [identity profile] sps.livejournal.com
perl -e 'for (sort {$a<=>$b} <*>) /\(\d+\)\.sthg/ and rename $_, (100+$1).".sthgelse"' ishly?

Date: 2009-02-14 07:53 pm (UTC)
From: [identity profile] joenotcharles.livejournal.com
Why the fetish for doing everything on one line? Put your script in a text file, run the text file, fix mistakes, delete the text file when you're done, or save it somewhere if you think it'll be useful again.

If it's complicated enough that it takes you significant brain power to figure out how to shove it into a one-liner, or to work out the escaping involved, then the extra time taken to write to and delete a text file goes toward saving you from that, and it makes debugging the inevitable mistakes so much easier.

Date: 2009-02-14 08:16 pm (UTC)
From: [identity profile] skjalm.livejournal.com
Agreed! Doing one liners (in whatever language) is fun, but sometimes it's not worth the extra hassle if the point of the exercise is to get something done rather than to do a nifty one liner ;)

That being said I couldn't help myself (see comment below) ;)

Date: 2009-02-14 08:28 am (UTC)
From: [identity profile] sps.livejournal.com
I'm afraid I'm not remembering the that. I never wore lead shorts, but then again, I haven't had any kids (that I know of) whereof to count the heads.

Date: 2009-02-14 08:28 am (UTC)
From: [identity profile] sps.livejournal.com
It has been a while.

Date: 2009-02-14 08:20 pm (UTC)
From: [identity profile] skjalm.livejournal.com
This is not very pretty, but gets the job done (example files shown before and after the move)

steelbook:foo steellynx$ ls
abc0	abc14	abc2	abc25	abc30	abc36	abc41	abc47	abc52	abc58
abc1	abc15	abc20	abc26	abc31	abc37	abc42	abc48	abc53	abc59
abc10	abc16	abc21	abc27	abc32	abc38	abc43	abc49	abc54	abc6
abc11	abc17	abc22	abc28	abc33	abc39	abc44	abc5	abc55	abc7
abc12	abc18	abc23	abc29	abc34	abc4	abc45	abc50	abc56	abc8
abc13	abc19	abc24	abc3	abc35	abc40	abc46	abc51	abc57	abc9
steelbook:foo steellynx$ find . -type f|perl -p -e 's/([^\d]*)(\d+)(.*)$/mv $1$2$3 `printf series_s%02dx%02d \$[$2%12] \$[$2\/12]`$3/g' | /bin/bash
steelbook:foo steellynx$ ls
series_s00x00	series_s02x02	series_s04x04	series_s07x01	series_s09x03
series_s00x01	series_s02x03	series_s05x00	series_s07x02	series_s09x04
series_s00x02	series_s02x04	series_s05x01	series_s07x03	series_s10x00
series_s00x03	series_s03x00	series_s05x02	series_s07x04	series_s10x01
series_s00x04	series_s03x01	series_s05x03	series_s08x00	series_s10x02
series_s01x00	series_s03x02	series_s05x04	series_s08x01	series_s10x03
series_s01x01	series_s03x03	series_s06x00	series_s08x02	series_s10x04
series_s01x02	series_s03x04	series_s06x01	series_s08x03	series_s11x00
series_s01x03	series_s04x00	series_s06x02	series_s08x04	series_s11x01
series_s01x04	series_s04x01	series_s06x03	series_s09x00	series_s11x02
series_s02x00	series_s04x02	series_s06x04	series_s09x01	series_s11x03
series_s02x01	series_s04x03	series_s07x00	series_s09x02	series_s11x04


As mentioned it's not pretty, but does have the advantage, imho, that I could build the mv commands and see that they looked sane before throwing them into a new shell. Plus... it's late so I have apparently made the classic mistake of swapping mod and div in the command *head+desk*

I'll shut up and go to bed now ;)

January 2017

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 13th, 2026 04:29 pm
Powered by Dreamwidth Studios