Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a file within recursive directory of zip files

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

  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.

What I have tried
1. I know that I can list files recursively pretty easily:

find myLostfile -type f

2. I know that I can list files inside zip archives:

unzip -ls myfilename.zip

How do I find a specific file within a directory structure of zip files?

like image 540
Alex Gordon Avatar asked May 08 '15 15:05

Alex Gordon


People also ask

How do I search for a file recursively?

Finding Files Recursively in Linux You only need to define the main directory and the file name using the –name option. This command will search the file within the main directory and all subdirectories. As you can see, the file was found in the /home/linuxhint/Downloads/recur subdirectory.

How do I recursive a zipped folder?

-r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.

Does grep work on zip files?

Unfortunately, grep doesn't work on compressed files. To overcome this, people usually advise to first uncompress the file(s), and then grep your text, after that finally re-compress your file(s)… You don't need to uncompress them in the first place. You can use zgrep on compressed or gzipped files.

How do I list all files in a directory recursively?

List files recursively using recursive ls option. Use find with -ls or -print option.


1 Answers

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 i in *.zip; do grep -iq "mylostfile" < <( unzip -l $i ) && echo $i; done

for recursive searching in bash 4:

shopt -s globstar
for i in **/*.zip; do grep -iq "mylostfile" < <( unzip -l $i ) && echo $i; done
like image 137
David C. Rankin Avatar answered Sep 21 '22 12:09

David C. Rankin