找到所有的拉链,并解压缩 - Unix [英] Find all zips, and unzip in place - Unix

查看:207
本文介绍了找到所有的拉链,并解压缩 - Unix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试一段时间,相信我已经相当接近这个了,但是对于Unix来说,我还是比较新的,所以一直觉得这很困难。

我有一个包含许多文件夹的文件夹,其中一些文件夹中包含zip文件,另一些则不包含。

例如我有:


p> files / A / something.java

files / B / somezipfile.zip

files / C / someotherfile.zip

files / D / AnotherZipFile.zip

我想解压缩它们(假设拉链只包含.java文件),得到如下结果:
files / A / something.java



files / B / javafile.java


$ b files / C / someotherfilefromzip.java

files / D / Anotherfile.java

我不介意ZIP文件是否保留或在解压缩后被删除,或者没有问题。



到目前为止我已经尝试过了。



1)我期望我可以使用管道,我是新来的,就像这样:

  find。 -name * .zip |解压缩

这不行。

2)我花了一些时间搜索,最接近我在网上使用的解决方案是:

  find。 -name'* .zip'-exec unzip'{}'';'

解压缩把它们解压缩到当前的工作目录中,我想让它们解压到位。我也不明白这个命令,我想我正在努力学习。



任何帮助都非常感谢。



谢谢,

解决方案

  find。 -name'* .zip'-exec sh -c'unzip -d`dirname {}`';'

这个命令在当前目录及其子目录中递归查找名称匹配 *。zip 模式的文件。找到的文件用两个参数执行 sh 命令:

  -c 

  unzip -d`dirname< filename>`< filename> 

其中< filename> 是找到的文件。命令 sh 是Unix shell解释器。选项 -c 告诉shell下一个参数应该被解释为shell脚本。所以shell解释下面的脚本:

pre $ uncip -d`dirname<

在运行 unzip shell之前展开命令,通过做各种替代。在这个特定的例子中,它替代了

 `dirname< filename>`
pre>

输出命令 dirname< filename> ,它实际上输出放置文件的目录名称。所以,如果文件名是 ./ a / b / c / d.zip ,shell会像这样擦 unzip

  unzip -d ./a/b/c ./a/b/c/d.zip 

如果您的ZIP文件名或目录名有空格,请使用以下命令:

  find。 -name* .zip-exec sh -c'unzip -d`dirname \{} \`{}'';'


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.

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.

For example I have:

files/A/something.java

files/B/somezipfile.zip

files/C/someotherfile.zip

files/D/AnotherZipFile.zip

I would like to unzip them (assuming the zips contain just .java files), to have a result like: files/A/something.java

files/B/javafile.java

files/C/someotherfilefromzip.java

files/D/Anotherfile.java

I don't mind if the ZIP files remain or are deleted after unzipping, either is fine.

What I have tried so far.

1) I expected I could use piping, which I am new to, like this:

find . -name *.zip | unzip

This doesn't work.

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.

Any help is much appreciated.

Thanks,

解决方案

find . -name '*.zip' -exec sh -c 'unzip -d `dirname {}` {}' ';'

This commands 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

and

unzip -d `dirname <filename>` <filename>

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>

Before running unzip shell expands the command, by doing various substitutions. In this particular example it substitutes

`dirname <filename>`

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 rub unzip like this:

unzip -d ./a/b/c ./a/b/c/d.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆