Tar archives are files that hold collections of other files, including information about them, such as ownership, permissions, and time stamps.
In the Linux operating system, you can use the tar command to create a tar archive. This command can also compress archives using various compression programs with gzip is the most popular algorithm.
By convention, the name of a tar archive compressed with gzip must end with .tar.gz or .tgz.
This article explains how to create a tar.gz file.
Creating a tar.gz file
Most Linux distributions come including GNU tar version which supports archive compression.
The general form of the command to create a tar.gz file is as follows:
tar -czf archive-name.tar.gz file-name...
This is what the command options mean:
-c – instruct tar to create a new archive.
-z – sets the compression method to gzip.
-f archive-name.tar.gz – specifies the name of the archive.
file-name … a list of files and directories separated by spaces to add to the archive.
The user who runs the command must have write permission in the directory where the tar.gz file will be created, and read the permissions on the file added.
For example, to create an archive named “archive.tar.gz” from “file1” and “file2” you would use the following command:
tar -czf archive.tar.gz file1 file2
If successful, the command does not print any output. To verify that the archive was created, list directory contents with ls.
Use the -v option to make the tar command more visible and print the file names added to the archive in the terminal.
If you want to make tar.gz in a specific directory, provide the full path to the archive file:
tar -czf /home/user/archive.tar.gz file1 file2
You can create a tar.gz file from the contents of one or more directories or files. By default, directories are archived recursively unless the –no-recursion option is specified.
The following example shows how to create an archive named “web_backup.tar.gz” from the / var / www / website directory:
tar -czf web_backup.tar.gz /var/www/website
If you are running a system that has an older tar version that does not support compression, you can use the gzip command:
tar -czf - file1 file2 | gzip > archive.tar.gz
In the example above, the tar command displays the archive to stdout (represented by -). This archive is distributed to gzip, which compresses and writes the archive to disk.
Create a tar.gz file from all “.jpg” files:
tar -czf images.tar.gz *.jpg
The wildcard (*) character means all files ending in the “.jpg” extension.
Create a tar.gz file, transfer via ssh and extract it on a remote machine:
tar cvf - project | ssh user@ip_addr "tar xv -C /var/www"
tar.gz file is a Tar archive compressed with Gzip. To create a tar.gz file, use the tar -czf command, followed by the name of the archive and file you want to add.
If you have questions, please leave a comment below.