Linux:解压缩包含同名文件的存档
我收到了一个 zip 文件,其中包含 40 个同名文件。 我想将每个文件提取到一个单独的文件夹或使用不同的名称(file1、file2 等)提取每个文件。
有没有办法使用标准 Linux 工具自动执行此操作? man unzip 的检查没有发现任何可以帮助我的东西。 zipsplit 似乎也不允许任意分割 zip 文件(我试图将 zip 分割成 40 个档案,每个档案包含一个文件)。
目前我正在单独(r)命名我的文件。对于 40 个文件的存档来说,这并不是什么大问题,但显然是不可扩展的。
有人有一个好的、简单的方法吗?比什么都好奇。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设目前不存在这样的工具,那么用 python 编写一个应该很容易。 Python 有一个 zipfile 模块 应该足够了。
像这样的东西(也许,未经测试):
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):
我知道这已经有几年了,但是上面的答案并没有解决我的特定问题,所以我想我应该继续发布一个对我有用的解决方案。
无需编写脚本,您只需使用命令行输入即可与解压缩工具文本界面进行交互。也就是说,当您在命令行中键入以下内容时:
并且它包含同名文件,它将提示您:
如果您想手动执行此操作,您将键入“r”,然后在下一个提示符下:
您只需输入新文件名即可。
要自动执行此操作,只需创建一个包含对这些提示的响应的文本文件,并将其用作解压缩的输入,如下所示。
使用您最喜欢的脚本语言可以很容易地生成它。将其另存为 unzip_input.txt,然后将其用作解压缩的输入,如下所示:
对我来说,这比尝试让 Perl 或 Python 提取模块按我需要的方式工作更让人头疼。希望这对某人有帮助...
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:
and it contains files of the same name, it will prompt you with:
If you wanted to do this by hand, you would type "r", and then at the next prompt:
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.
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:
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...
这是一个Linux脚本版本,
在这种情况下,834733991_T_ONTIME.csv是每个zip文件中相同的文件名,“$count”后面的.csv只需与您想要的文件类型交换
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
该线程很旧,但仍有改进的空间。就我个人而言,我更喜欢 bash 中的以下一行,这是
一种很好、干净、简单的方法来删除扩展并使用
This thread is old but there is still room for improvement. Personally I prefer the following one-liner in bash
Nice, clean, and simple way to remove the extension and use the
使用
unzip -B file.zip
为我解决了这个问题。如果文件已存在,它会创建一个后缀为~
的备份文件。例如:
注意:
-B
选项不会出现在unzip --help
中,但在手册页中提到:https://manpages.org/unzip#optionsUsing
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:
Note: the
-B
option does not show up inunzip --help
, but is mentioned in the man pages: https://manpages.org/unzip#options