How to Search Compressed Files Using Linux

Look through your zipped files without opening them

This guide will show you how to search compressed files on a Linux computer for a string of text or a particular expression.

How to Search and Filter Results Using the grep Command

One of the most powerful Linux commands is grep, which stands for Global Regular Expressions Print. You can use grep to search for patterns within the contents of a file or the output from another command.

As an example, run the following ps command to see the list of processes that are running on your computer:

ps -ef

The results scroll on the screen quickly. If there are a large number of results, this makes viewing the information confusing.

Linux ps command

To stop the page from scrolling, use the more command to list one page of results at a time, as follows:

ps -ef | more

While the output from the above command is better than the previous one, you still have to page through the results to find what you need.

The grep command makes it possible to filter the results based on the criteria you send to it. For example, to search for all processes with the UID set to root, run the following command:

ps -ef | grep root
Linux ps with grep

The grep command also works on files. Imagine you have a file that contains a list of book titles, and you want to see if the file contains Little Red Riding Hood. You can search the file as follows:

grep "Little Red Riding Hood" booklist
Linux grep file

The grep command is powerful, and many useful switches can be used with it.

How to Search Compressed Files Using the zgrep Command

A little known but very powerful tool is zgrep. The zgrep command lets you search the contents of a compressed file without extracting the contents first. The zgrep command can be used on zip files or files compressed using the gzip command.

What is the difference? A zip file can contain multiple files, whereas a file compressed using the gzip command only contains the original file.

To search for text within a file compressed with gzip, enter the following command:

zgrep expression filetosearch

For example, imagine the books list was compressed using gzip. You can search for the text little red riding hood in the compressed file using the following command:

zgrep "Little Red Riding Hood" bookslist.gz
Linux zgrep

You can use any expression, and all the settings available with the grep command as part of the zgrep command.

How to Search Compressed Files Using the zipgrep Command

The zipgrep command is used to search for patterns within a zip file. The zgrep command works well with files compressed using gzip but doesn't work so well on files compressed using the zip utility. You can use zgrep if the zip file contains a single file, but most zip files contain more than one file.

As an example, imagine you have a file called books with the following titles:

  • Harry Potter and the Chamber of Secrets
  • Taming of the Shrew
  • Of Mice and Men
  • The Hitchhikers Guide to the Galaxy
  • Harry Potter and the Order of the Phoenix

Also, imagine you have a file called movies with the following titles:

  • The Matrix
  • Harry Potter and the Chamber of Secrets
  • Harry Potter and the Goblet of Fire
  • Star Wars: A New Hope

Now, imagine these two files have been compressed using the zip format into a file called media.zip. You can use the zipgrep command to find patterns in the files within the zip file. For example:

zipgrep pattern filename

To find all the occurrences of Harry Potter, use the following command:

zipgrep "Harry Potter" media.zip

The output is as follows:

books:Harry Potter and the Chamber of Secrets
books:Harry Potter and the Order of the Phoenix
movies:Harry Potter and the Chamber of Secrets
movies:Harry Potter and the Goblet of Fire
Linux zipgrep

As you can use any expression with zipgrep that you can use with grep, this makes the tool very powerful, and it makes searching zip files simpler than decompressing, searching, and then compressing again.

If you only want to search certain files within the zip file, specify the files to search within the zip file as part of the command, as follows:

zipgrep "Harry Potter" media.zip movies

The output is as follows:

movies:Harry Potter and the Chamber of Secrets
movies:Harry Potter and the Goblet of Fire
Linux zipgrep search by file

If you want to search all of the files except for one, use the following command:

zipgrep "Harry Potter" media.zip -x books

This produces the same output as before as it searches all files within media.zip except for books.