Archives compressed with gzip have the
.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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the |
|Conventions|| # - requires given linux commands to be executed with root privileges either directly as a root user or by use of |
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.
- To list all the contents of a compressed gzip archive (or any kind of tar file), use the
-toption with tar. We'll also need the
-foption, 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
- To get more details about the files, such as size, permissions, owner, etc, you can add the
$ tar -tvf Linux-ISOs.tar.gz
- 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
- 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
- You can also pipe the
tar -tfcommand 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
-ioption in our grep command just makes the search case insensitive.
$ tar -tf Linux-ISOs.tar.gz | grep -i man
- 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"
- 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
- 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
$ zgrep Ubuntu *.gz
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.
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.
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.