bash 在 zip 文件的递归目录中查找文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30127764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding a file within recursive directory of zip files
提问by l--''''''---------''''''''''''
I have an entire directory structure with zip files. I would like to:
我有一个包含 zip 文件的完整目录结构。我想要:
- Traverse the entire directory structure recursively grabbing all the zip files
- I would like to find a specific file "*myLostFile.ext" within one of these zip files.
- 递归遍历整个目录结构抓取所有zip文件
- 我想在这些 zip 文件之一中找到一个特定的文件“*myLostFile.ext”。
What I have tried
1. I know that I can list files recursively pretty easily:
我尝试过的
1. 我知道我可以很容易地递归列出文件:
find myLostfile -type f
2. I know that I can list files inside zip archives:
2. 我知道我可以列出 zip 档案中的文件:
unzip -ls myfilename.zip
How do I find a specific file within a directory structure of zip files?
如何在 zip 文件的目录结构中找到特定文件?
回答by David C. Rankin
You can omit using find for single-level (or recursive in bash 4 with globstar
) searches of .zip
files using a for
loop approach:
您可以省略使用 find 进行单级(或在 bash 4 中使用 递归globstar
).zip
使用for
循环方法搜索文件:
for i in *.zip; do grep -iq "mylostfile" < <( unzip -l $i ) && echo $i; done
for recursive searching in bash 4:
在 bash 4 中递归搜索:
shopt -s globstar
for i in **/*.zip; do grep -iq "mylostfile" < <( unzip -l $i ) && echo $i; done
回答by Eric Renouf
You can use xargs
to process the output of find or you can do something like the following:
您可以使用xargs
来处理 find 的输出,或者您可以执行以下操作:
find . -type f -name '*zip' -exec sh -c 'unzip -l "{}" | grep -q myLostfile' \; -print
which will start searching in .
for files that match *zip
then will run unzip -ls
on each and search for your filename. If that filename is found it will print the name of the zip file that matched it.
这将开始搜索.
匹配的文件,*zip
然后unzip -ls
在每个文件上运行并搜索您的文件名。如果找到该文件名,它将打印与其匹配的 zip 文件的名称。