If you frequently navigate the open-source world, you are likely to come across .tar.gz files often. Generally, open-source packages can be downloaded in .tar.gz or .zip formats. The
tar command is used to create tar archives, which can group multiple files into an archive. It can support a broad range of compression programs like gzip, bzip2, lzip, lzma, lzop, xz and compress. Initially, Tar was designed to create archives to store files on magnetic tape, which is why it is called “Tape ARchive.”
Gzip is the most popular algorithm for compressing tar files. Typically, the name of a tar archive compressed with gzip should end with either .tar.gz or .tgz. In essence, a file that ends in .tar.gz is a .tar archive compressed with gzip. The tar command can also perform other operations like extracting tar archives, displaying a list of files included in the archive, and adding more files to an existing archive.
This tutorial will show you how to extract or unzip tar.gz and tgz archives in Linux and Windows. Follow these steps to learn how to use
tar command for unzipping and extracting files.
How to Unpack tar.gz File
By default, the tar command is pre-installed on most Linux distributions and macOS systems. To extract a tar.gz file, use the –extract (-x) option, followed by the archive file name after the f option. For example, to extract a file named “archive.tar.gz,” use the command “tar -xf archive.tar.gz.” This command is an essential tool for managing compressed files in Unix-based operating systems.
tar -xf file_name.tar.gz
If you need to extract a tar archive, you can use the “tar” command, which automatically detects the compression type and extracts the archive, including those compressed with algorithms such as .tar.bz2. To make the files being extracted more visible, use the -v option. For example, to extract the contents of a “archive.tar.gz” file, you can run the following command in the terminal:
tar -xvf file_name.tar.gz
If you prefer a graphical interface, you can use your file manager. To extract a tar.gz file on a desktop environment, right-click on the file you want to extract and select “Extract.” Windows users will need to use a tool like 7zip to extract tar.gz files. By default, the tar command extracts the archive contents in the current working directory. To extract the archive files in a specific directory, use the –directory (-C) option. For instance, if you want to extract the archive contents to the /home/trioguide/files directory, run this command:
tar -xf file_name.tar.gz -C /home/trioguide/files
Using these methods, you can extract tar archives quickly and easily.
How to Extract Specific Files from a tar.gz File
To extract individual files from a tar.gz archive, add a list of file names separated by spaces after the archive name, like this:
tar -xf archive.tar.gz file1 file2
Make sure to include the exact file names and paths as shown by the –list (-t) option when extracting files.
If you want to extract directories instead of individual files, use the same command:
tar -xf archive.tar.gz dir1 dir2
To extract a file using the tar command, you need to ensure that the file exists. Attempting to extract a non-existent file will trigger an error message that looks like this: “tar -xf archive.tar.gz README”. Ensure that the file you want to extract is available before attempting to extract it using the tar command.
tar -xf archive.tar.gz README
Ouputtar: README: Not found in archive
tar: Exiting with failure status due to previous errors
tar -xf archive.tar.gz --wildcards '*.js'
How to Extracr tar.gz File from stdin
To extract a compressed tar.gz file while reading the archive from stdin, it’s crucial to specify the decompression option. The -z option instructs tar to read the archives through gzip. Let’s consider an example where we download the Blender sources using the wget command and pipe its output to the tar command. By using this method, we can efficiently extract the compressed file without any issues.
wget -c https://ftp.halifax.rwth-aachen.de/blender/release/Blender3.5/blender-3.5.0-linux-x64.tar.xz -O - | sudo tar -xz
To ensure optimal usage of the tar command, it is essential to specify the appropriate decompression option. However, in cases where the option is not specified, tar will automatically suggest the suitable option to use. This feature of tar can aid in simplifying the decompression process and save valuable time.
tar: Archive is compressed. Use -z option
tar: Error is not recoverable: exiting now
How to List tar.gz file
If you want to list the content of a tar.gz file, use the
tar -tf archive.tar.gz
The output will look like this:
To obtain additional details, like the owner, timestamp, file size, and more, you can utilize the –verbose (-v) flag when using tar. You can execute the following command to list the contents of the archive file archive.tar.gz with verbose output:
tar -tvf archive.tar.gz
By using this command with the –verbose option, you can efficiently manage your archive files with ease.
-rw-r--r-- trioguide/users 0 2023-03-31 02:20 file1
-rw-r--r-- trioguide/users 0 2023-03-31 02:20 file2
-rw-r--r-- trioguide/users 0 2023-03-31 02:20 file3
-rw-r--r-- trioguide/users 0 2023-03-31 02:20 file4
To extract a tar.gz file, you can utilize the tar -xf command along with the name of the compressed archive. A tar.gz file is essentially a Gzip-compressed Tar archive. In case you need further assistance or have any queries, feel free to leave a comment below.