开发者

Linux: Unzip an archive containing files with the same name

开发者 https://www.devze.com 2022-12-08 15:58 出处:网络
I was sent a zip file containing 40 f开发者_运维百科iles 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)

I was sent a zip file containing 40 f开发者_运维百科iles 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.


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()


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...


here is a linux script version

in this case the 834733991_T_ONTIME.csv is the name of the file that is the same inside every zip file, and the .csv after "$count" simply has to be swapped with the file type you want

#!/bin/bash

count=0

for a in *.zip
do
    unzip -q "$a"
    mv 834733991_T_ONTIME.csv "$count".csv
    count=$(($count+1))
done`


This thread is old but there is still room for improvement. Personally I prefer the following one-liner in bash

unzipd ()
{
    unzip -d "${1%.*}" "$1"
}

Nice, clean, and simple way to remove the extension and use the

0

精彩评论

暂无评论...
验证码 换一张
取 消