How to ungzip or untar tar.gz file? [SOLVED]


Linux, Tips and Tricks

Author: Omer Cakmak
Reviewer: Deepak Prasad

You may have encountered the ".gz, .tgz, .tar.gz" file extensions in the Linux operating system before. These extensions may be more familiar when it comes to installing applications from source code and you want to view logs. Because Linux Operating systems prefer these file extensions in the file compression process and they give output.

So, how can we untar tar.gz files? Can we unzip tar.gz files? Which application we can use to perform ungzip operation?

You will find answers to these questions in this article.

 

Install tar and gzip/gunzip(ungzip) Package

To use gunzip, the gzip package is installed. In fact, tar and gzip packages come pre-installed on many operating systems. Some operating systems give packages like "sudo", "gnome" as dependent packages. Therefore, it may not be possible to remove them. So how to install tar and gzip(gunzip) packages? You can find the answer to this question below.

Install to Redhat based operating systems(Fedora, Centos, AlmaLinux, Rocky Linux, etc):

sudo dnf -y install gzip tar

or

sudo yum -y install gzip tar

Install to Debian-based operating systems (Ubuntu, Pardus, Linux Mint, Kali Linux, etc.):

sudo apt install gzip tar -y

or

sudo apt-get install gzip tar -y

Install to on Arch Linux:

sudo pacman -S gzip tar

Install to on openSUSE Leap:

sudo zypper install gzip tar

We learned how to install packages according to Linux distributions. We have 2 ways to open tar.gz files (tar, gunzip). Let's make it more understandable with examples.

 

Method-1: Untar tar.gz files using tar command

Extract all content with tar command, extract a specific file and list the files without extracting them.

 

Example-1: Extract tar.gz or .gz archive

To extract a tar.gz or gz archive we need to use following set of arguments.

Here,

  • -x is to extract the tar file
  • -z is used to perform gunzip or extract .gz archive
  • -v is to get verbose output
  • -f to provide the archive file

Here I have a sample archive, let us try to extract it:

~]# tar -xzvf example.tar.gz 
myssh
hy.txt
infra.manifest

As you can see, the contents of the archive are successfully extracted to the current folder.

 

Example-2: Extract the content into a different directory or path

We saw in the last example that by default tar will extract content into the current folder. If we want to specify the destination directory where the content should be extracted and kept then we need to use -C or --directory argument.

Here you can see that we are extracting the content of our archive into /tmp/temp.Jsuv directory instead of the current path.

~]# tar -xzvf example.tar.gz -C /tmp/temp.JsUv/
myssh
hy.txt
infra.manifest

~]# ls -l /tmp/temp.JsUv/
total 24
-rw-------. 1 root root    51 Oct 20 12:45 hy.txt
-rw-------. 1 root root 15254 Aug 23 08:06 infra.manifest
-rw-------. 1 root root  3618 Oct 27 09:18 myssh

 

Example-3: Extract multiple tar.gz archives recursively

Here I have a wrapper archive which internally contains 2 nested gzip archives.

~]# tar -tzvf wrapper_archive.tar.gz 
-rw------- root/root      7471 2022-10-27 09:19 test/example1.tar.gz
-rw------- root/root       530 2022-10-27 09:26 test/example2.tar.gz

To extract the wrapper and the nested archives we have to use the following command:

~]# tar --to-command='tar -xzvf -' -xzvf wrapper_archive.tar.gz
test/example1.tar.gz
myssh
hy.txt
infra.manifest
test/example2.tar.gz
test.yaml
testscript.py

~]# ls -l
total 44
-rw-------. 1 root root    51 Oct 20 12:45 hy.txt
-rw-------. 1 root root 15254 Aug 23 08:06 infra.manifest
-rw-------. 1 root root  3618 Oct 27 09:18 myssh
-rw-------. 1 root root   373 Oct 21 12:11 testscript.py
-rw-------. 1 root root   328 Oct 10 10:35 test.yaml
-rw-------. 1 root root  8215 Oct 27 09:29 wrapper_archive.tar.gz

 

Example-4: Extract specific file(s)

To extract a specific file we have to provide the file name which we need to extract from the archive. Use the following syntax:

tar -xzvf<archive.tar.gz> <filename>

But to be able to know the filename, we must check the content of the archive. So we can list the content of the archive without actually extracting everything by using -tz argument where -t is for --list i.e. list the contents of the archive.

# tar -tzvf example.tar.gz 
-rw------- root/root      3618 2022-10-27 09:18 myssh
-rw------- root/root        51 2022-10-20 12:45 hy.txt
-rw------- root/root     15254 2022-08-23 08:06 infra.manifest

Now that we know the content of our archive, we can extract myssh file from this archive without extracting other contents.

~]# tar -xzvf example.tar.gz myssh 
myssh

~]# ls -l myssh 
-rw-------. 1 root root 3618 Oct 27 09:18 myssh

Similarly we can extract more than one files at a time by specifying the list of files separated by comma

~]# tar -xzvf example.tar.gz myssh hy.txt
myssh
hy.txt

~]# ls -l myssh hy.txt 
-rw-------. 1 root root   51 Oct 20 12:45 hy.txt
-rw-------. 1 root root 3618 Oct 27 09:18 myssh

 

Example-5: List files from the archive without extraction

We have briefly touched this point in our previous examples. Sometimes you may not need the file to open, you may want to check it and open it later. Run the below command in the terminal to see the contents of a tar.gz file without extracting it:

foc@fedora:~/unzip_folder$ tar -tf example_folder.tar.gz
example_folder/
example_folder/example_file1
example_folder/example_file3
example_folder/example_file4
example_folder/example_file5
example_folder/example_file2

 

Method-2: Unzip .gz archive using gunzip command

Let's do the steps we mentioned above with gunzip.

 

Example-1: Extract .gz archive content

We can use -d or --decompress or --uncompress to extract a .gz archive:

~]# gunzip -dv script.sh.gz 
script.sh.gz:	 80.0% -- replaced with script.sh

~]# ls -l
total 12
-rw-------. 1 root root 8618 Oct 27 10:16 script.sh

 

Example-2: Extract multiple .gz archives recursively

To extract multiple gz archives we can place them inside a directory and then use -r or --recursive to perform the extraction recursively.

 temp.JsUv]# ls -l
total 20
-rw-------. 1 root root  210 Oct 27 10:20 post-init.log.gz
-rw-------. 1 root root 1751 Oct 27 10:20 script.sh.gz

temp.JsUv]# cd ..

tmp]# gunzip -r temp.JsUv/

tmp]# ls -l temp.JsUv/
total 16
-rw-------. 1 root root  286 Oct 27 10:20 post-init.log
-rw-------. 1 root root 8618 Oct 27 10:20 script.sh

As you can see, both our gz archives are extracted.

 

Example-3: List files inside gz archive without extraction

View gunzip file content without extracting. The -l parameter shows the compressed file contents without extracting:

# gunzip -l post-init.log.gz 
         compressed        uncompressed  ratio uncompressed_name
                210                 286  37.8% post-init.log

 

Summary

File compression and extraction is part of system administration. You must perform these operations using the applications that come with the operating system itself. This is the most stable and safe method.

For more detailed information about these commands, you can visit the manual pages.

You can get help locally with the --help command:

foc@fedora:~/unzip_folder$ tar --help
Usage: tar [OPTION...] [FILE]...
GNU 'tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.

Examples:
  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
  tar -tvf archive.tar         # List all files in archive.tar verbosely.
  tar -xf archive.tar          # Extract all files from archive.tar.

 Main operation mode:
  -A, --catenate, --concatenate   append tar files to an archive
  -c, --create               create a new archive
      --delete               delete from the archive (not on mag tapes!)
  -d, --diff, --compare      find differences between archive and file system
  -r, --append               append files to the end of an archive
      --test-label           test the archive volume label and exit
  -t, --list                 list the contents of an archive
  -u, --update               only append files newer than copy in archive
  -x, --extract, --get       extract files from an archive
...

For gunzip:

foc@fedora:~/unzip_folder$ gunzip --help
Usage: /usr/bin/gunzip [OPTION]... [FILE]...
Uncompress FILEs (by default, in-place).

  -c, --stdout      write on standard output, keep original files unchanged
  -f, --force       force overwrite of output file and compress links
  -k, --keep        keep (don't delete) input files
  -l, --list        list compressed file contents
  -n, --no-name     do not save or restore the original name and timestamp
  -N, --name        save or restore the original name and timestamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
...

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment