A tar is an archive file (
.tar) to store collection of files and directories. A tar archive file can be compressed using various algorithms. The gzip algorithm is one of the most popular. By convention,
.tgz extension is used when a tar archive file is compressed with gzip. This tutorial shows how to create tar gz file in Linux.
tar command can be used to create tar archive files. A syntax to create
tar czf filename.tar.gz directory_path1 directory_path2 ...
- before the options (
czf) is optional for
tar command. Used options:
c- specifies to create an archive file.
z- compress archive file with gzip algorithm (remove this option to create a tar file without compression).
f- specifies filename of archive file.
For example, the following command allows to create
backup.tar.gz file from
sudo tar czf backup.tar.gz /var/log
.tar.gz file can be created by specifying multiple files and directories:
sudo tar czf backup.tar.gz /etc/hosts /etc/crontab /home