在zip文件的递归目录中查找文件

时间:2021-07-10 23:13:11

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. 遍历整个目录结构以递归方式获取所有zip文件

  3. I would like to find a specific file "*myLostFile.ext" within one of these zip files.
  4. 我想在其中一个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文件的目录结构中找到特定文件?

2 个解决方案

#1


You can omit using find for single-level (or recursive in bash 4 with globstar) searches of .zip files using a for loop approach:

您可以使用for循环方法省略使用find进行单级(或使用globstar的bash 4递归)搜索.zip文件:

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

#2


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文件的名称。

#1


You can omit using find for single-level (or recursive in bash 4 with globstar) searches of .zip files using a for loop approach:

您可以使用for循环方法省略使用find进行单级(或使用globstar的bash 4递归)搜索.zip文件:

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

#2


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文件的名称。