Linux Programming

Zipgrep – How to search through zip files in Linux

zipgrep
zipgrep

Zipgrep is an amazing tool to search through zip archive for a specified pattern. Zipgrep is just a piece of shell script which leverage the usage of unzip and egrep to run. Zipgrep process given expressions just as egrep.

Zipgrep – Usage

Since it’s a just a wrapper script for unzip and grep, it’s input and output process is a lot similar to the egrep . Zipgrep searches for text strings inside the files contained in the archive, not the filenames that zip archive contains. Note that the command syntax is:search pattern + archivename + optional list of filenames to search.

#zipgrep pattern filename

For instance if you have a file named “h-books” with the following list:

  • Metasploit: The penetration tester’s guide
  • Hacking: The Art of Exploitation, 2nd Edition
  • The Basics of hacking and Penetration Testing
  • CEH Certified Ethical Hackers All-in-one Exam Guide
  • Black Hat Python: Python Programming for Hackers and pen-testers

And you also have another file called “l-books” with the following list:

  • How Linux Works: What Every Superuser Should Know by Brian Ward
  • The Linux Programming Interface: A Linux and UNIX System Programming Handbook by Michael Kerrisk
  • Unix and Linux System Administration Handbook by Evi Nemeth
  • Linux in a Nutshell: A Desktop Quick Reference
  • The Linux Command Line – A Complete Introduction by Williams E. Shotts

Now these two files have been compressed using the zip format into a file called “library.zip. Now you can use the zipgrep command to find patterns within all the files within the zip file.

For example, if you wanted to search for all the occurrences of “hacking” you would use the following command:

zipgrep "hacking" library.zip

The result will be:

[user@hostname ~]$ zipgrep "Hacking" library.zip
library/h-books:Hacking: The Art of Exploitation, 2nd Edition
library/h-books:The Basics of Hacking and Penetration Testing
[user@hostname ~]$ 

zipgrep-example

As you can see, you can use any expression with zipgrep that you use with grep or egrep, this makes the zipgrep tool very handy, and it makes looking for zip files much easier than decompressing, searching and then compressing again.

If you only want to search specific files within the zip archive you can give the certain files names to search within the zip archive as part of the command shown below:

zipgrep "Linux" library.zip l-books

If you want to exclude the certain files from your search process, you can use the following command as an example:

zipgrep "Program" library.zip -x h-books

This will result in same output as before as it will search all files within library.zip except for excluded file “h-books”.

About the author

Ajay Verma

A Computer Science Graduate, who works extensively on open source projects. His Areas Of interest are: Network Security, Linux Administration, FOSS, Python, and C programming.

Add Comment

Click here to post a comment