Search the contents of compressed gzip archive file on Linux

Archives compressed with gzip have the .tar.gz or .tgz file extension. It’s easy enough to extract the contents from these files, but what if you only need a certain file? There’s not much sense in extracting hundreds or thousands of files from an archive if you’re only looking for a few files.

Fortunately, we can utilize the Linux command line and even GUI archive managers to search the contents of gzip compressed archives. Once we identify the file we want, it’s possible to extract the file by itself, rather than extracting every single file.

In this guide, we’ll show how to search one or multiple gzip archives for a particular file from both command line and GUI.

In this tutorial you will learn:

  • How to search the contents of a compressed gzip archive via command line
  • How to search the contents of a compressed gzip archive via GUI
  • How to search the contents of multiple gzip archives
  • How to extract a particular file from a gzip archive

Searching for two different file name patterns in a compressed archive on Linux

Searching for two different file name patterns in a compressed archive on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software tar, gzip
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Search the contents of gzip archive via command line



Check out some of the command line examples below and try them out on your own system to learn how to search for a file inside a gzip archive. If you prefer to use the GUI instead, you can skip ahead to the next section of the article.

  1. To list all the contents of a compressed gzip archive (or any kind of tar file), use the -t option with tar. We’ll also need the -f option, since we will specify the name of our tar file after. This will return a full list of all the files and directories that reside inside the archive.
    $ tar -tf Linux-ISOs.tar.gz
    
  2. Listing all the contents of a compressed archive

    Listing all the contents of a compressed archive
  3. To get more details about the files, such as size, permissions, owner, etc, you can add the -v option.
    $ tar -tvf Linux-ISOs.tar.gz
    


  4. Listing all the contents of a compressed archive, along with extra file details

    Listing all the contents of a compressed archive, along with extra file details
  5. This is simple enough, but for some archives you could potentially have thousands of files and directories to sift through. To search for a particular file, you can append the name of it to your command. If the file doesn’t exist in the archive, you won’t receive any output returned to your terminal.
    $ tar -tf Linux-ISOs.tar.gz Slackware.iso
    
  6. Searching a compressed gzip archive for a particular file

    Searching a compressed gzip archive for a particular file
  7. You can also use wildcards in your file search, in case you want to search for a particular pattern.
    $ tar -tf Linux-ISOs.tar.gz *.iso
    
  8. Searching a compressed archive for a file name pattern

    Searching a compressed archive for a file name pattern


  9. You can also pipe the tar -tf command to grep. This might be a little better, as grep will search for any files that match your search string, rather than only matching whole file names. Note that the -i option in our grep command just makes the search case insensitive.
    $ tar -tf Linux-ISOs.tar.gz | grep -i man
    
  10. Using grep command to search for a file name pattern in the compressed archive

    Using grep command to search for a file name pattern in the compressed archive
  11. Or we can search for a couple of different files at the same time, by using the following command syntax with grep.
    $ tar -tf Linux-ISOs.tar.gz | grep -E "Slack|Pop"
    
  12. Searching for two different file name patterns in a compressed archive

    Searching for two different file name patterns in a compressed archive
  13. Another option we have is to use the vim text editor to browse the contents of a compressed gzip archive.
    $ vim Linux-ISOs.tar.gz
    


  14. Browsing the contents of a compressed archive with vim text editor

    Browsing the contents of a compressed archive with vim text editor
  15. The easiest way to search multiple gzip archives at once would be with the zgrep utility, which is basically just grep for compressed archives. The following example is able to determine that a file matching the pattern “Ubuntu” is inside the Debian-based.tar.gz file archive.
    $ zgrep Ubuntu *.gz
    
  16. Using zgrep utility to search multiple compressed archives for a file name pattern

    Using zgrep utility to search multiple compressed archives for a file name pattern

Once you have identified the file that you wish to extract, you can extract the specific file from the gzip archive.

$ tar xvf Debian-based.tar.gz Ubuntu.iso

This should be all the commands you need in order to search one or multiple gzip compressed archives for a particular file, or a naming pattern. There are more methods available than just the ones we listed here, but we found these to be the easiest and most effective when testing. See the section below if you’d also like to learn how to search a gzip archive from GUI.

Search the contents of gzip archive via GUI



Opening and browsing gzip archives via GUI will vary a little, depending on the desktop environment you have installed, or what Linux distro you’re running. However, all modern installations of Linux will certainly have a default program that can open compressed tar archives, including gzip archives.

Browse to where you have your compressed archive stored, then double click the file to open it, or right click on the file and choose to open it with your system’s archive manager.

Opening a compressed archive via GUI

Opening a compressed archive via GUI

Once the archive is open, you click the magnifying glass (or however you search icon is represented) and type part of the file name to search for what you want.

Specifying a search pattern in GUI archive manager

Specifying a search pattern in GUI archive manager

Closing Thoughts

In this guide, we saw how to search the contents of a compressed gzip archive. This included command line methods that can search for a specific file or a pattern. We also learned how to search multiple compressed archives at the same time. Lastly, we saw how the same functionality can be achieved with a GUI’s archive manager. These examples should help you to quickly identify and extract any file or set of files from compressed archives.



Comments and Discussions
Linux Forum