为目录中的每个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?
推荐答案
在此处提取"仅是您使用的任何unzip
包装器的功能. unzip
将仅提取存档中的实际内容.可能没有比shell脚本更简单的方法了.但是,如果您具有POSIX兼容的外壳,则不需要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屋!