Ubuntu – How to batch rename a specific file inside multiple zip archives via the command line

batch-renamezip

I have about 200+ shareware files in zip format that each contain a file called "FILE_ID.DIZ". I need to know how to rename each instance to lowercase "file_id.diz" without doing it manually – I've already gone that route and it's pretty time consuming. That file has to be lowercase because the BBS program I'm using ignores the FILE_ID.DIZ as a description since it is uppercase. If I manually change it to lowercase, the description is imported successfully.

I know that rar has a renaming switch, but then I'd have to batch convert all of the zip files to rar, and then back to zip. I'm not ruling that out entirely, but it seems like the long way around to resolving this.

I found the man page for "zip_rename", which looks like it might do the trick, but I have no idea how to actually implement it.

I refuse to do this on a Windows machine – I just can't and won't do it… it's the principle ;).

Anyway, thanks for your time!

Best Answer

This technique won't rename the file, but add a lowercase version of the .DIZ file. Hopefully it will be good enough for your purposes.

This takes advantage of zip's "add by default" functionality, in which if you specify an existing zip file, it will add files to it. So if you do:

zip doom.zip file_id.diz

and doom.zip exists, it will just add the file_id.diz file to the existing contents.

This is to be run in the directory where your zip files reside:

for i in  *zip; do rm FILE_ID.DIZ;  unzip "$i" FILE_ID.DIZ; mv FILE_ID.DIZ file_id.diz; zip -d "$i" FILE_ID.DIZ; zip "$i" file_id.diz; done

Note that for best results, none of the .zip file names should contain spaces.

You can also put this in a file, say, rename.sh (and it looks nicer and easier to understand this way):

#!/bin/bash
for i in  *zip; do
   rm FILE_ID.DIZ
   unzip "$i" FILE_ID.DIZ
   mv FILE_ID.DIZ file_id.diz
   zip -d "$i" FILE_ID.DIZ
   zip "$i" file_id.diz
done

then you can run by saying, e.g. bash rename.sh