为目录中的每个 zip 文件创建一个专用文件夹并解压 zip 文件
如果我选择一个 zip 文件并右键单击“在此处提取”,则会创建一个具有 zip 文件名的文件夹,并将 zip 文件的全部内容提取到其中。
但是,我想通过 shell 转换几个 zip 文件。 但是,当我执行
unzip filename.zip
此操作时,不会创建文件夹 "filename"
但所有文件都会提取到当前目录中。
我查看了参数,但没有这样的参数。 我也尝试过
for zipfile in \*.zip; do mkdir $zipfile; unzip $zipfile -d $zipfile/; done
,但是 2. $zipfile 和 4. $zipfile 的 .zip
扩展名必须用 sed 删除。 如果我
for zipfile in \*.zip; do mkdir sed 's/\.zip//i' $zipfile; unzip $zipfile -d sed 's/\.zip//i' $zipfile/; done
这样做是行不通的。
如何正确替换 $zipfile
的 .zip
扩展名?
有没有比 shell 脚本更简单的方法?
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.
However, I would like to convert several zip files via shell.
But when I do
unzip filename.zip
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
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
it is not working.
How do I replace the .zip
extension of $zipfile
properly?
Is there an easier way than a shell script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
unzip file.zip -d xxx
会将文件解压到目录 xxx 中,如果 xxx 不存在,则会创建该目录。您可以查看手册页了解详细信息。下面的 awk 行应该可以完成这项工作:
请参阅下面的测试,
注意我在最后删除了
|sh
,因为我的拉链是假的档案;我只想在这里显示生成的命令行。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.The awk line below should do the job:
See the test below,
note that I removed
|sh
at the end, since my zips are fake archives; I just want to show the generated command line here.“在此处提取”只是您正在使用的任何
unzip
包装器的一个功能。unzip
只会提取存档中实际存在的内容。可能没有比 shell 脚本更简单的方法了。但是,如果您有符合 POSIX 标准的 shell,则不需要 sed、awk 等:(您不得转义
*
或路径名不会发生扩展。)请注意,如果 ZIP 存档包含目录,例如 Eclipse 存档(始终包含eclipse/
),则最终会得到./eclipse* /eclipse/eclipse.ini
在任何 案件。在unzip
之前添加echo
以进行试运行。"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. Butsed
,awk
etc. are not needed for this if you have a POSIX-compliant shell:(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 containeclipse/
), you would end up with./eclipse*/eclipse/eclipse.ini
in any case. Addecho
beforeunzip
for a dry run.p7zip
,7zip 的命令行版本可以完成这项工作。在 Debian 和 Ubuntu 上,您必须安装
p7zip-full
。p7zip
, the command line version of 7zip does the jobOn Debian and Ubuntu, you have to install
p7zip-full
.在开关后添加文件夹名称和斜杠,例如:
zip 将创建文件夹“newfolder”并将存档解压到其中。
请注意,尾部斜杠不是必需的,但我想这只是旧习惯(我使用 Debian 和 Ubuntu)。
Add the folder name and slash after the switch, example:
zip will create the folder 'newfolder' and extract the archive into it.
Note that the trailing slash is not required but I guess it's just from old habit (I use Debian and Ubuntu).
默认情况下,atool 中的
aunpack
将对所有类型的存档执行此操作。aunpack
from atool will do this by default for all sorts of archives.在 Termux bash 中,我最终得到了这个,测试了文件名中的空格和点。它仅删除最后一个文件扩展名和文件夹名称的点。这假设 zip 文件具有 .zip 扩展名,否则它将无法工作。在意识到它们只需要作为普通引号存在之前,我疯狂地试图转义引号或使用单引号。在 zip 命令前面添加 echo,或添加 -l 来审核该命令。由于某种原因,-d 的位置对于这个实现来说似乎很重要。
In Termux bash I ended up with this, tested for spaces and dots in filenames. It only removes the last file extension and dot for the folder name. This assumes the zip file has a .zip extension otherwise it won't work. Went nuts trying to \ escape the quotes or use single quotes before realizing they just needed to be there as plain quote marks. Drop an echo in front of the zip command, or add a -l to audit the command. For some reason the position of the -d seems important for this implementation.
等待进程结束,这取决于文件大小。
wait until the process ends, it depends on the file size.