1. Overview
The .tar.gz file format is a common way to compress and archive files. It can be used to store large amounts of data within a small storage space. Also, .tar.gz package files are usually easier to transfer and share.
Sometimes, we may need to view the contents of a tar.gz file without extracting it. This can be useful if we want to see what files are included in the archive, or if we want to preview a file without having to extract the entire archive. Hence, this also saves time by only listing files instead of extracting them.
In this tutorial, we’ll see a few different ways to view the contents of tar.gz files without extracting them.
Notably, we’ve used a Ubuntu 22.04 system to run the commands in this tutorial. However, the commands and tools we use are available on most Linux systems.
2. Using the tar Command
The tar command is a powerful tool for managing archive files. In particular, it can be used to create, extract, list, and modify archives.
To view the contents of a .tar.gz file without extracting it, we can use the tar command with the -t option. For example, let’s view the contents of the file myfile.tar.gz:
$ tar -tf myfile.tar.gz
AAA/
AAA/mmm/
AAA/mmm/ttt.txt
AAA/mmm/ooo.txt
AAA/kkk/
In this case, the -f option specifies the archive file name. As a result, we see all of the files that are included in the archive.
We can also use the -v option to get more verbose output:
$ tar -tvf myfile.tar.gz
drwxrwxr-x user/user 0 2023-07-06 11:01 AAA/
drwxrwxr-x user/user 0 2023-07-06 11:02 AAA/mmm/
...
As we see, this output also includes the size and permissions of each file.
3. Using the less Command
The less command is a pager, which means that it allows us to scroll through the contents of a file and other output one screen at a time.
Let’s use less to view the contents of our .tar.gz file:
$ less myfile.tar.gz
This opens the .tar.gz file on the terminal and lists the archive contents:
drwxrwxr-x user/user 0 2023-07-06 11:01 AAA/
drwxrwxr-x user/user 0 2023-07-06 11:02 AAA/mmm/
-rw-rw-r-- user/user 1700 2022-03-31 10:57 AAA/mmm/ttt.txt
-rw-rw-r-- user/user 1700 2022-03-31 10:57 AAA/mmm/ooo.txt
drwxrwxr-x user/user 0 2023-07-06 11:01 AAA/kkk/
myfile.tar.gz (END)
Also, we can use the arrow keys to navigate through the file, and press q to exit the program.
4. Using the vim Command
At its core, Vim is a text editor. However, we can also use it for many other purposes, including to view archived files.
Let’s use Vim to see the contents of our .tar.gz file:
$ vim myfile.tar.gz
" tar.vim version v32
" Browsing tarfile /home/user/Downloads/test/myfile.tar.gz
" Select a file with cursor and press ENTER
AAA/
AAA/mmm/
AAA/mmm/ttt.txt
AAA/mmm/ooo.txt
AAA/kkk/
The vim command opens the .tar.gz file in the Vim editor, so we can check the file listing.
Furthermore, we have the j and k keys to scroll through the file. Also, we can press the colon (🙂 followed by q to exit Vim.
5. Using the lesspipe Command
The lesspipe command is a preprocessor for the less pager, designed to enhance its capabilities. For instance, lesspipe enables less pager to process a variety of file formats.
Indeed, lesspipe allows displaying the contents of various archive files without unarchiving them beforehand. Additionally, unlike the less command, lesspipe prints the entire output to stdout.
Let’s use the lesspipe command to list the contents of our .tar.gz file:
$ lesspipe myfile.tar.gz
drwxrwxr-x user/user 0 2023-07-06 11:01 AAA/
drwxrwxr-x user/user 0 2023-07-06 11:02 AAA/mmm/
-rw-rw-r-- user/user 1700 2022-03-31 10:57 AAA/mmm/ttt.txt
-rw-rw-r-- user/user 1700 2022-03-31 10:57 AAA/mmm/ooo.txt
drwxrwxr-x user/user 0 2023-07-06 11:01 AAA/kkk/
Just like less, lesspipe is available on most Linux distributions.
6. Using Midnight Commander
Midnight Commander is a file manager that can be used to browse and manage files and directories.
Usually, we can install Midnight Commander using the package manager specific to our Linux distribution. For example, on Ubuntu 22.04, we can use the general apt tool:
$ sudo apt install mc
To use Midnight Commander and view the contents of a .tar.gz file, we can follow several steps:
- Open Midnight Commander
- Select a panel (left or right) using the Tab key
- Navigate to the destination directory
- Select the target .tar.gz file
- Use the Enter key to open the file
Once the file is open, we can see the archived files and directories:
To exit Midnight Commander, we press the Esc key followed by 0.
7. Conclusion
In this article, we’ve explored different methods to view the contents of a .tar.gz file without extracting it. Let’s quickly sum them up.
Firstly, we saw the tar command, which is generally used for managing compressed archives. Secondly, we utilized the less command, useful for working with large text files. Thirdly, we discussed the vim command, a pretty straightforward approach. Then, we moved on to the lesspipe command.
Finally, we got to Midnight Commander, a GNU-based visual file manager. It provides a convenient and user-friendly experience.
Overall, listing the contents of an archive without extracting it offers several advantages such as saving time, conserving disk space, and reducing disk I/O overhead.