为目录中的每个 zip 文件创建一个专用文件夹并提取 zip 文件 [英] Create a dedicated folder for every zip files in a directory and extract zip files
问题描述
如果我选择一个 zip 文件并右键单击在此处提取",则会创建一个带有 zip 文件名的文件夹,并将 zip 文件的全部内容提取到其中.
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.
但是,我想通过 shell 转换几个 zip 文件.但是当我这样做
However, I would like to convert several zip files via shell. But when I do
unzip filename.zip
文件夹"filename"
没有被创建,但是所有的文件都被解压到了当前目录中.
the folder "filename"
is not created but all the files are extracted into the current directory.
我查看了参数,但没有这样的参数.我也试过
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
但是必须使用 sed 删除 2. $zipfile 和 4. $zipfile 的 .zip
扩展名.如果我这样做
but the .zip
extension of the 2. $zipfile and 4. $zipfile have to be removed with sed.
If I do
for zipfile in *.zip; do mkdir sed 's/.zip//i' $zipfile; unzip $zipfile -d sed 's/.zip//i' $zipfile/; done
它不起作用.
如何正确替换$zipfile
的.zip
扩展名?
How do I replace the .zip
extension of $zipfile
properly?
有没有比 shell 脚本更简单的方法?
Is there an easier way than a shell script?
推荐答案
extract here"只是您使用的任何 unzip
包装器的一个功能.unzip
只会提取存档中的实际内容.可能没有比 shell 脚本更简单的方法了.但是如果你有一个符合 POSIX 的 shell,则不需要 sed
、awk
等:
"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:
for f in *.zip; do unzip -d "${f%*.zip}" "$f"; done
(您不得转义 *
否则路径名扩展不会发生.)请注意,如果 ZIP 存档包含目录,例如 Eclipse 存档(始终包含 eclipse/
),无论如何都会得到 ./eclipse*/eclipse/eclipse.ini
.在 unzip
之前添加 echo
以进行试运行.
(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 文件创建一个专用文件夹并提取 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!