找到所有的压缩包,并在原地解压 - Unix [英] Find all zips, and unzip in place - Unix
问题描述
我已经尝试了一段时间,相信我已经很接近这个了,但是我对 Unix 还很陌生,所以一直觉得这很困难.
I have been trying for some time and believe I am fairly close to this, but I am fairly new to Unix so have been finding this difficult.
我有一个文件夹,其中包含许多文件夹,其中一些包含 zip 文件,而另一些则没有.我正在尝试解压缩任何子目录中的所有 zip 文件.
I have a folder, containing many folders, some of which have zip files in them, some which don't. I am trying to unzip all of the zip files in any sub directories in place.
例如我有:
文件/A/something.java
files/A/something.java
文件/B/somezipfile.zip
files/B/somezipfile.zip
文件/C/someotherfile.zip
files/C/someotherfile.zip
文件/D/AnotherZipFile.zip
files/D/AnotherZipFile.zip
我想解压缩它们(假设 zip 仅包含 .java 文件),得到如下结果:文件/A/something.java
I would like to unzip them (assuming the zips contain just .java files), to have a result like: files/A/something.java
文件/B/javafile.java
files/B/javafile.java
files/C/someotherfilefromzip.java
files/C/someotherfilefromzip.java
files/D/Anotherfile.java
files/D/Anotherfile.java
我不介意ZIP文件在解压缩后是保留还是删除,都可以.
I don't mind if the ZIP files remain or are deleted after unzipping, either is fine.
到目前为止我已经尝试过什么.
What I have tried so far.
1) 我希望我可以使用管道,我是新手,像这样:
1) I expected I could use piping, which I am new to, like this:
find . -name *.zip | unzip
这不起作用.
2)我花了一些时间搜索,我使用在线解决方案最接近的是:
2) I spent some time searching, the closest I got using a solution online is:
find . -name '*.zip' -exec unzip '{}' ';'
这会解压缩,但会将它们解压缩到当前工作目录中,我希望它们解压缩到位.我也不明白这个我想学习的命令.
This unzips, but unzips them into the current working directory, I wanted them to unzip in place. I also don't understand this command which I would like to as I am trying to learn.
非常感谢任何帮助.
谢谢,
推荐答案
find . -name '*.zip' -exec sh -c 'unzip -d `dirname {}` {}' ';'
此命令在当前目录及其子目录中递归查找名称与 *.zip
模式匹配的文件.对于找到的文件,它使用两个参数执行命令 sh
:
This command looks in current directory and in its subdirectories recursively for files with names matching *.zip
pattern. For file found it executes command sh
with two parameters:
-c
和
unzip -d `dirname <filename>` <filename>
其中 <filename>
是找到的文件的名称.命令 sh
是 Unix shell 解释器.选项 -c
告诉 shell 下一个参数应该被解释为 shell 脚本.所以shell解释了以下脚本:
Where <filename>
is name of file that was found. Command sh
is Unix shell interpreter. Option -c
tells shell that next argument should be interpreted as shell script. So shell interprets the following script:
unzip -d `dirname <filename>` <filename>
在运行 unzip
之前,shell 通过执行各种替换来扩展命令.在这个特定的例子中,它替代了
Before running unzip
shell expands the command, by doing various substitutions. In this particular example it substitutes
`dirname <filename>`
使用命令 dirname <filename>
的输出,该命令实际输出放置文件的目录名称.所以,如果文件名是 ./a/b/c/d.zip
,shell 会像这样运行 unzip
:
with output of command dirname <filename>
which actually outputs directory name where file is placed. So, if file name is ./a/b/c/d.zip
, shell will run unzip
like this:
unzip -d ./a/b/c ./a/b/c/d.zip
如果您的 ZIP 文件名或目录名有空格,请使用:
In case you ZIP file names or directory names have spaces, use this:
find . -name '*.zip' -exec sh -c 'unzip -d "`dirname "{}"`" "{}"' ';'
这篇关于找到所有的压缩包,并在原地解压 - Unix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!