How to Extract tar.gz File in Linux

Channel: Linux
Abstract: directories and subdirectories under the /var/log directory. tar -czf Backup.tar.gz /var/logand all files under the log directory. You can also specif

A .tar.gz is compression format commonly used in the Unix based operating system. The tar.gz file format is the combination of TAR packaging followed by the GNU zip (gzip) compression.

The tar command is used to compress multiple files and create a single archive file. It creates highly compressed file to save more disk space. The same command is also used to extract, maintain, or modify tar archives via command line .

This tutorial will help you with the followings:

  1. How to extract .tar.gz file
  2. How to create a .tar.gz file
  3. And list .tar.gz file content without extracting
How to extract .tar.gz file

Use tar command with -x (extract), -z (gzip) and -f for the file name. The below example command will extract content of Backup.tar.gz file in the current directory.

tar -xzf Backup.tar.gz 

This will silently extract .tar.gz file in current directory. Use -v to enable verbose mode.

To extract content to other directory us option -C (capital C). This will change the extract location of files.

tar -xvzf Backup.tar.gz -C /opt  

The above command will extract all files under /opt directory.

How to create .tar.gz file

Use -c command line option to create a tar archive. The -z option is used to compress with TAR + Gzip.

The following command will compress all files, directories and subdirectories under the /var/log directory.

tar -czf Backup.tar.gz /var/log  

In case of absolution path, GNU tar also maintains the directory structure but skipped root (/) in file system. In the above case the archive contains structure as 「var/log/」 and all files under the log directory.

You can also specify multiple files names to add them in single archive.

tar -czf Backup.tar.gz *.log /backup/*.sql /opt/list.txt 

The above command will archive all files with .log extension in current directory. All files with .sql in /backup directory and /opt/list.txt. As the absolute path is used, it will remove the / directory. You will see the message like 「tar: Removing leading `/’ from member names」

How to list .tar.gz file content

Use -t option to list all files compressed under a .tar.gz file without extracting them. For example, execute on previously created compressed files:

tar -tvzf Backup.tar.gz  


-rw-r--r-- root/root    119783 2019-12-31 06:22:22 file1.txt
-rw------- root/root    863287 2019-08-14 17:35:58 anaconda.log
-rw------- root/root         0 2019-12-29 02:17:27 boot.log
-rw-r--r-- root/root         0 2019-12-29 02:17:26 kdm.log
-rw-r--r-- root/root     25643 2019-12-29 01:12:35 Xorg.0.log
-rw-r--r-- root/root     11380 2019-11-24 17:35:39 yum.log
Conclusion

This tutorial explained you to how to create and extract .tar.gz file on Linux command line. It also helped you to view .tar.gz file content without extracting it.

For more details visit tar command man pages.

Ref From: tecadmin
Channels: targzip

Related articles