简体   繁体   中英

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.

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

Using unzip -B file.zip did the trick for me. It creates a backup file suffixed with ~<number> in case the file already exists.

For example:

$ rm *.xml
$ unzip -B bogus.zip
Archive:  bogus.zip
  inflating: foo.xml
  inflating: foo.xml
  inflating: foo.xml
  inflating: foo.xml
  inflating: foo.xml
$ ls -l
-rw-rw-r--  1 user user    1161 Dec 20 20:03 bogus.zip
-rw-rw-r--  1 user user    1501 Dec 16 14:34 foo.xml
-rw-rw-r--  1 user user    1520 Dec 16 14:45 foo.xml~
-rw-rw-r--  1 user user    1501 Dec 16 14:47 foo.xml~1
-rw-rw-r--  1 user user    1520 Dec 16 14:53 foo.xml~2
-rw-rw-r--  1 user user    1520 Dec 16 14:54 foo.xml~3

Note: the -B option does not show up in unzip --help , but is mentioned in the man pages: https://manpages.org/unzip#options

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM