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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:57:49  来源:igfitidea点击:

Finding a file within recursive directory of zip files

linuxbashubuntusedgrep

提问by l--''''''---------''''''''''''

I have an entire directory structure with zip files. I would like to:

我有一个包含 zip 文件的完整目录结构。我想要:

  1. Traverse the entire directory structure recursively grabbing all the zip files
  2. I would like to find a specific file "*myLostFile.ext" within one of these zip files.
  1. 递归遍历整个目录结构抓取所有zip文件
  2. 我想在这些 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 .zipfiles using a forloop 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 xargsto 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 *zipthen will run unzip -lson 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 文件的名称。