Create Tar Gz File in Linux

Create Tar Gz File in Linux

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, .tar.gz or .tgz extension is used when a tar archive file is compressed with gzip. This tutorial shows how to create tar gz file in Linux.

The tar command can be used to create tar archive files. A syntax to create .tar.gz file:

tar czf filename.tar.gz directory_path1 directory_path2 ...

Adding - 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 /var/log directory:

sudo tar czf backup.tar.gz /var/log

The .tar.gz file can be created by specifying multiple files and directories:

sudo tar czf backup.tar.gz /etc/hosts /etc/crontab /home

Leave a Comment

Cancel reply

Your email address will not be published.