Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a set of archive files -- both zip and rar formats -- inside of which I need to rename some files. Specifically, I want to do something like this:

for each archive file in a directory
    for each file in the archive
        if the file name matches the regular expression /(.* - [0-9]{2})([0-9]{2} - .)*/
            rename the file as \1-\2

The trick isn't so much in the generation of the new name; I can do that with either bash or sed or anything else. It's the set of commands to manipulate the files in the archives using rar/unrar or unzip/zip

(If it makes a difference, I'm re-formatting some CBR/CBZ files to get the double-page spreads to come up in the right order in SimpleComic -- it interprets page 0203 as page 203, which makes the story a bit hard to follow)

share|improve this question
up vote 1 down vote accepted

I think the easiest way you'll find to do this is to unpack each archive, do the rename operation, then repack it.

On Linux, I could use a FUSE filesystem to have transparently decompressed archives, which I could operate on like regular folders, using a batch script to do all the renaming. I don't know if that particular FUSE filesystem exists for Mac.

share|improve this answer
Which fuse FS is that? – Chris R Jan 28 '10 at 23:07
Here's a list of candidates:… I haven't actually used any myself. I just said that I could. – Ryan Thompson Jan 30 '10 at 23:18

I look ate you example and I see PYTHON CODE :)

Really I think this should be the way to go. I know this isn't stackoverflow.... but this is the only way I can see your problem solved.

Example code to extract all files:

import zipfile

a = zipfile.ZipFile("C:\\", 'r')
for i in a.namelist():         #Extract every file from it
     b = open("C:\\"+i, 'wb')

Hope this helps

This page teaches how to instal an unrar for the command line for MAC.

So you can do a script to unrar and a script to rar your files and call then from python, and make python change the names.

To execute an external script use:

import os;
share|improve this answer
It does, but here's the trick; zipfile doesn't support RAR archives. – Chris R Mar 9 '10 at 16:07
True, but there's a rarfile package which does: – Tim Gilbert Apr 28 '13 at 19:07

rar has a command line switch to rename files in the archive:

rar rn archive.rar filename newfilename

however the zip utility you're using does not. You'll need to extract, rename, and recompress as Ryan stated. There are other zip utilities for Mac which probably have this capability. I know 7-zip has supported renaming in archives since 2.30 beta 25, although I haven't checked if they support it from the command line. 7-zip is fairly extensive when it comes to command line options, so I'd assume it can.

share|improve this answer

I've been looking into this too, and I suspect the easiest thing to do is probably write a python script to do it, especially because you'll be dealing with both RAR and ZIP files.

It looks like there's not a particularly clean way to rename a file in a zip archive without unzipping all the files, renaming the ones you want to rename, and then zipping them back up, which is not really ideal. However, I did stumble upon a OS/X utility called zipnote which does purport to do this:

zipnote man page

It doesn't look terribly easy to use, but it might be worth looking into, and assuming it doesn't actually unzip the files itself it should be fairly easy to automate. Meanwhile, the command-line OS/X rar utility does support renaming files inside of RAR archives via rar rn foobar.rar oldname newname.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .