Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux: Unzip an archive containing files with the same name

I was sent a zip file containing 40 files with the same name. I wanted to extract each of these files to a seperate folder OR extract each file with a different name (file1, file2, etc).

Is there a way to do this automatically with standard linux tools? A check of man unzip revealed nothing that could help me. zipsplit also does not seem to allow an arbitrary splitting of zip files (I was trying to split the zip into 40 archives, each containing one file).

At the moment I am (r)enaming my files individually. This is not so much of a problem with a 40 file archive, but is obviously unscalable.

Anyone have a nice, simple way of doing this? More curious than anything else.

Thanks.

like image 963
pisswillis Avatar asked Oct 13 '09 16:10

pisswillis


People also ask

How do I unzip an archive file in Linux?

To unzip files, open File Manager, as explained in the Zipping Files via GUI section. Right click the ZIP package you'd like to extract, and select Extract Here, as shown below. Once you click Extract Here, Linux will extract all files in the ZIP package in the working directory.

How do I extract a separated zip file?

To unzip filesOpen File Explorer and find the zipped folder. To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it.


2 Answers

Assuming that no such tool currently exists, then it should be quite easy to write one in python. Python has a zipfile module that should be sufficient.

Something like this (maybe, untested):

#!/usr/bin/env python

import os
import sys
import zipfile

count = 0

z = zipfile.ZipFile(sys.argv[1],"r")

for info in z.infolist():
    directory = str(count)
    os.makedirs(directory)
    z.extract(info,directory)
    count += 1

z.close()
like image 193
Douglas Leeder Avatar answered Oct 17 '22 02:10

Douglas Leeder


I know this is a couple years old, but the answers above did not solve my particular problem here so I thought I should go ahead and post a solution that worked for me.

Without scripting, you can just use command line input to interact with the unzip tools text interface. That is, when you type this at the command line:

unzip file.zip

and it contains files of the same name, it will prompt you with:

replace sameName.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename:

If you wanted to do this by hand, you would type "r", and then at the next prompt:

new name:

you would just type the new file name.

To automate this, simply create a text file with the responses to these prompts and use it as the input to unzip, as follows.

r
sameName_1.txt
r
sameName_2.txt
...

That is generated pretty easily using your favorite scripting language. Save it as unzip_input.txt and then use it as input to unzip like this:

unzip < unzip_input.txt

For me, this was less of a headache than trying to get the Perl or Python extraction modules working the way I needed. Hope this helps someone...

like image 3
tbh Avatar answered Oct 17 '22 02:10

tbh