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
Create a dedicated folder for every zip files in a directory and extract zip files
提问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 .zip
extension of the 2. $zipfile and 4. $zipfile have to be removed with sed.
If I do
但是.zip
2. $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 .zip
extension of $zipfile
properly?
如何正确更换.zip
扩展名$zipfile
?
Is there an easier way than a shell script?
有没有比 shell 脚本更简单的方法?
采纳答案by PointedEars
"extract here" is merely a feature of whatever unzip
wrapper you are using. unzip
will only extract what actually is in the archive. There is probably no simpler way than a shell script. But sed
, awk
etc. are not needed for this if you have a POSIX-compliant shell:
“在此处提取”只是unzip
您使用的任何包装器的一个功能。 unzip
只会提取存档中的实际内容。可能没有比 shell 脚本更简单的方法了。但是sed
,awk
如果您有符合 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.ini
in any case. Add echo
before unzip
for a dry run.
(您*
不得转义 ,否则将不会发生路径名扩展。)请注意,如果 ZIP 存档包含一个目录,例如 Eclipse 存档(始终包含eclipse/
),则无论如何您最终./eclipse*/eclipse/eclipse.ini
都会得到 。添加echo
之前unzip
进行试运行。
回答by Kent
unzip file.zip -d xxx
will 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 |sh
at 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
aunpack
from 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)。