Linux 为目录中的每个 zip 文件创建一个专用文件夹并提取 zip 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8107886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:10:13  来源:igfitidea点击:

Create a dedicated folder for every zip files in a directory and extract zip files

linuxshellsedawkunzip

提问by creativeDev

If I choose a zip file and right click "extract here" a folder with the zip filename is created and the entire content of the zip file is extracted into it.

如果我选择一个 zip 文件并右键单击“在此处提取”,则会创建一个带有 zip 文件名的文件夹,并将 zip 文件的全部内容提取到其中。

However, I would like to convert several zip files via shell. But when I do

但是,我想通过 shell 转换几个 zip 文件。但是当我这样做时

unzip filename.zip

the folder "filename"is not created but all the files are extracted into the current directory.

"filename"未创建文件夹,但所有文件都被提取到当前目录中。

I have looked at the parameters but there is no such parameter. I also tried

我查看了参数,但没有这样的参数。我也试过

for zipfile in \*.zip; do mkdir $zipfile; unzip $zipfile -d $zipfile/; done

but the .zipextension of the 2. $zipfile and 4. $zipfile have to be removed with sed. If I do

但是.zip2. $zipfile 和 4. $zipfile的扩展名必须用 sed 删除。如果我做

for zipfile in \*.zip; do mkdir sed 's/\.zip//i' $zipfile; unzip $zipfile -d sed 's/\.zip//i' $zipfile/; done 

it is not working.

它不工作。

How do I replace the .zipextension of $zipfileproperly?

如何正确更换.zip扩展名$zipfile

Is there an easier way than a shell script?

有没有比 shell 脚本更简单的方法?

采纳答案by PointedEars

"extract here" is merely a feature of whatever unzipwrapper you are using. unzipwill only extract what actually is in the archive. There is probably no simpler way than a shell script. But sed, awketc. are not needed for this if you have a POSIX-compliant shell:

“在此处提取”只是unzip您使用的任何包装器的一个功能。 unzip只会提取存档中的实际内容。可能没有比 shell 脚本更简单的方法了。但是sedawk如果您有符合 POSIX 标准的外壳,则不需要,等等:

for f in *.zip; do unzip -d "${f%*.zip}" "$f"; done

(You MUST NOT escape the *or pathname expansion will not take place.) Be aware that if the ZIP archive contains a directory, such as with Eclipse archives (which always contain eclipse/), you would end up with ./eclipse*/eclipse/eclipse.iniin any case. Add echobefore unzipfor a dry run.

(您*不得转义 ,否则将不会发生路径名扩展。)请注意,如果 ZIP 存档包含一个目录,例如 Eclipse 存档(始终包含eclipse/),则无论如何您最终./eclipse*/eclipse/eclipse.ini都会得到 。添加echo之前unzip进行试运行。

回答by Kent

unzip file.zip -d xxxwill extract files to directory xxx, and xxx will be created if it is not there. You can check the man page for details.

unzip file.zip -d xxx将文件解压到目录xxx,如果xxx不存在,则会创建xxx。您可以查看手册页了解详细信息。

The awk line below should do the job:

下面的 awk 行应该可以完成这项工作:

ls *.zip|awk -F'.zip' '{print "unzip "
kent$  ls -l
total 0
-rw-r--r-- 1 kent kent 0 Nov 12 23:10 001.zip
-rw-r--r-- 1 kent kent 0 Nov 12 23:10 002.zip
-rw-r--r-- 1 kent kent 0 Nov 12 23:10 003.zip
-rw-r--r-- 1 kent kent 0 Nov 12 23:10 004.zip
-rw-r--r-- 1 kent kent 0 Nov 12 23:10 005.zip
-rw-r--r-- 1 kent kent 0 Nov 12 23:10 006.zip
-rw-r--r-- 1 kent kent 0 Nov 12 23:10 007.zip

kent$  ls *.zip|awk -F'.zip' '{print "unzip "
7z x '*.zip' -o'*'
" -d "}' unzip 001.zip -d 001 unzip 002.zip -d 002 unzip 003.zip -d 003 unzip 004.zip -d 004 unzip 005.zip -d 005 unzip 006.zip -d 006 unzip 007.zip -d 007
" -d "}'|sh

See the testbelow,

看下面的测试

notethat I removed |shat the end, since my zips are fake archives; I just want to show the generated command line here.

请注意,我最后删除|sh了,因为我的 zip 是假档案;我只想在这里显示生成的命令行。

unzip -d newfolder/ zipfile.zip

回答by Franz Knülle

p7zip, the command line version of 7zip does the job

p7zip, 7zip 的命令行版本可以完成这项工作

##代码##

On debian and ubuntu, you have to install p7zip-full.

在 debian 和 ubuntu 上,您必须安装p7zip-full.

回答by kralyk

aunpackfrom atool will do this by default for all sorts of archives.

aunpack默认情况下,来自 atool 的所有类型的档案都会执行此操作。

回答by Jnnno

Add the folder name and slash after the switch, example:

在 switch 后添加文件夹名称和斜杠,例如:

##代码##

zip will create the folder 'newfolder' and extract the archive into it.

zip 将创建文件夹“newfolder”并将存档解压缩到其中。

Note that the trailing slash is not required but I guess it's just from old habit (I use Debian and Ubuntu).

请注意,尾部斜杠不是必需的,但我想这只是出于旧习惯(我使用 Debian 和 Ubuntu)。