Jun 19, 2023 4 min read

How to Create Tar.Gz File

Create Tar Gz File with our tutorial. It is used for compressing and archiving multiple files or directories into a single file.

Create Tar.Gz File
Table of Contents

Before we begin talking about how to create tar.gz file, let's briefly understand – What is tar.gz?

tar.gz is a common file format used for compressing and archiving multiple files or directories into a single file. The "tar" part stands for "tape archive," while "gz" refers to the compression method, Gzip. By combining files and reducing their size, tar.gz files make it easier to store, transfer, and backup data.

They are widely supported across various operating systems and are commonly used in software distribution, backups, and data transfers. Understanding tar.gz files is essential for managing and manipulating compressed data efficiently.

In this tutorial, we will show you how to create tar.gz files. We will also address a few FAQs on how to create tar.gz file.

Advantages of tar.gz

  1. Compression: tar.gz efficiently reduces file sizes, saving storage space and reducing transfer times.
  2. File Combination: It allows multiple files and directories to be combined into a single archive for easy management.
  3. Cross-Platform Compatibility: tar.gz files are widely supported across different operating systems, ensuring seamless file sharing.
  4. Integrity Preservation: tar.gz maintains file permissions, timestamps, and symbolic links, preserving data integrity during archiving and extraction.
  5. Versatile Usage: tar.gz is commonly used for software distribution, backups, and data transfers, making it a versatile file format for various applications.

Create tar.gz File

Most Linux distributions include the GNU version of tar, which supports archive compression.

The following is the general syntax for creating tar.gz files:

tar -czf archive-name.tar.gz file-name...

Here’s what the command options mean:

  • -c - tells tar to create a new archive.
  • -z - sets the compression method to gzip.
  • -f archive-name.tar.gz - sets the archive name.
  • file-name... a space-separated list of files and directories to be added to the archive.

The user running the command must have read and write permissions on the files being added, as well as write permissions on the directory where the tar.gz file will be created.

To make an archive named "archive.tar.gz" from "file1" and "file2," for example, use the following command:

tar -czf archive.tar.gz file1 file2

When the command succeeds, no output is printed. To ensure that the archive has been created, use ls to list the contents of the directory.

🤔
To make the tar command more visible, use the -v option, which prints the names of the files being added to the archive on the terminal.

If you want the tar.gz to be created in a specific directory, specify the full path to the archive file:

tar -czf /home/user/archive.tar.gz file1 file2

Tar.gz files can be generated from the contents of one or more directories or files. Unless the --no-recursion option is specified, directories are archived recursively by default.

The following example shows how to make a "web backup.tar.gz" archive of the /var/www/website directory:

tar -czf web_backup.tar.gz /var/www/website

If you have an older version of tar that does not support compression, you can use the gzip command:

tar -czf - file1 file2 | gzip > archive.tar.gz

In the preceding example, the tar command writes the archive to stdout (represented by -). The archive is piped to gzip, which compresses and writes it to disc.

Examples

  • Create a tar.gz file from all “.png” files:
tar -czf images.tar.gz *.png

The wildcard character (*) represents all files with the extension ".jpg."

  • Create a tar.gz file, ssh it to the remote machine, and extract it:
tar cvf - project | ssh user@ip_addr "tar xv -C /var/www"

FAQs to Create Tar.Gz File

How can I compress files into a Tar.gz file with maximum compression?

To achieve maximum compression, add the z option when creating the Tar.gz file. For instance, tar czvfz archive.tar.gz file1.txt dir1 will use the highest compression level.

Can I include subdirectories when creating a Tar.gz file?

Yes, you can include subdirectories by specifying the parent directory that contains them when creating the Tar.gz file. All files and subdirectories within the specified parent directory will be included.

What happens if a file with the same name already exists in the Tar.gz file?

When creating a Tar.gz file, if a file with the same name already exists, the new file will overwrite the existing one within the archive.

Can I create a Tar.gz file from a specific directory?

Yes, you can create a Tar.gz file from a specific directory by specifying the directory's path when using the tar command.

How do I extract files from a Tar.gz file?

To extract files from a Tar.gz file, use the command tar xzvf followed by the file name. This will extract all files and directories from the archive.

Can I extract specific files from a Tar.gz file?

Yes, you can extract specific files from a Tar.gz file by specifying their names or paths after the tar xzvf command. For example, tar xzvf archive.tar.gz file1.txt will extract only file1.txt from the archive.

Are Tar.gz files platform-independent?

Yes, Tar.gz files are platform-independent, meaning they can be created and extracted on different operating systems such as Windows, macOS, and Linux, ensuring compatibility and easy file sharing.

Conclusion

A tar.gz file is a Gzip-compressed Tar archive. Use the tar -czf command, followed by the archive name and the files you want to add, to create a tar.gz file.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.