A compressed archive file format called a tar.gz file is widely used in Linux and Unix-based operating systems. It combines the Gzip compression format and the TAR file format, which are two different file formats. The Gzip file format is used to compress the archive and minimize its size for effective storage and transfer. The TAR file format is used to bundle numerous files and directories into a single archive file. If you need something that should help and act as a version control tool and help you work on other actions then learn to to Stash an Untracked File in Git.
The ability to compress big files and directories into a single archive file, making it simpler to transport them over networks or keep them on external devices, is one benefit of utilizing tar.gz files. Additionally, the compression reduces the amount of disc space needed, especially when working with big amounts of data.
Using the Linux command line, you can extract a tar.gz file to a different directory by using the -C option and the directory path. the following steps:
- Activate the terminal window.
- Use the cd command to move to the directory containing the tar.gz file.
- Use the tar command with the arguments -xvf followed by the filename and the -C option followed by the directory path where you want to extract the contents if you want to extract the file to a separate directory.
The command would be as follows, for instance, if the file name is example.tar.gz and you wish to extract the files to the directory /home/user/documents/:
tar -xvf example.tar.gz -C /home/user/documents/
The tar.gz file’s contents will be extracted and placed in the directory you specified once the command has been executed.
How to Extract a tar.gz File
The tar command is typically pre-installed on Linux distributions by default.
tar xvzf file.tar.gz
X: When selected, tar is instructed to extract the files.
v: Verbose output displays every file that is being extracted.
Z: Instructs tar to decompress the archive using gzip.
F: The command’s final flag must be this one. The name and path of the compressed file are provided to tar.
File tar.gz Extraction to a Different Directory
tar xvzf file.tar.gz -C /tmp/archive
The current working directory is not the only directory that can be specified using the -C option.
List of Files to view within an Archive
Because tar files collect numerous files and check to see if a certain file is present, you may occasionally need to inspect their contents.
Use the following syntax to view a thorough table of contents for the archive data.tar.gz:
tar tf data.tar.gz
Where:
t: Used to list the contents of the tar file
f: Instructing the tool to utilize the file specified in the following argument
You can also get detailed standard output by using the v (verbose) option.
tar tvf data.tar.gz
Wrapping up on Unzip/Extract tar.gz Files Command in Linux
You now understand how to use Linux to untar a tar.gz file. How to unzip files in Linux might be something else that interests you. Consult the tar command’s manual page for more information.
Hope this article is useful for the queries you had regarding extracting the tar.gz files in Linux and anything related to that. Tried to explain in detail and by the steps of an easy way to make the confusion clear to everyone who needs to understand it.
Frequently Asked Questions (FAQ)
1. What is a .tar.gz file?
.tar.gz file merges numerous files or directories into a single .tar file, then compresses it using gzip to reduce its size, resulting in a .tar.gz extension. A .tar.gz file is a compressed archive in Linux.
2. Can I Extract a .tar.gz File on any Linux Distribution?
Yes, the tar command is versatile across Linux allocations, so the procedure should function on most systems.