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.
It provide highly compressed files to save disk space. Which is also easier to transfer between two systems.
This tutorial will help you with the followings.
- Extract content of .tar.gz file
- Compress files in a .tar.gz file
- List files in .tar.gz file without extracting
Here you go:
1. 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
To extract content to other directory us option -C (capital C). This will change the extract location of files.
The below example will extract all the files under /opt directory.
tar -xzf Backup.tar.gz -C /opt
2. Create A .tar.gz File
Use -c command line option to create a tar archive. The -z option is used to compress with TAAR + 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”
3. List .tar.gz Content without Extract
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
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.