How to Create a Tar Gz File in Linux operating system

How to Create Tar Gz File in Linux operating system

This article is going to show you that how to Create Tar Gz File in Linux operating system. If you want to learn that how to Create Tar Gz File in Linux then this article is ideal for you.

In computing, A tar file is a software utility for collecting many files into one single file for easy storage, often called as a tarball, for backup purposes. Tar files are compressed after being created and it is save with .tar.gz file extension.

In the Linux, if you want to create a tar archives then you can use the tar command to create it. The  tar command also compress the tar archives using a compression programs with gzip is the most popular algorithm.

Creating tar.gz File in Linux

The basic command for the creating tar.gz files is as below:

$ tar -czf archivename.tar.gz filename...

Here’s :

  • -c –> is tells tar to create a new archive.
  • -z –> is sets the compression method to gzip.
  • -f archive-name.tar.gz –> specifies the name of archive.
  • filename...> represent a list of files and directories to be added into the archive with space-separated. (for example :- filename1 filename2 )

For Example :

As a example, we create a Tar File named “example.tar.gz” with “filename1” and “filename2

$ tar -czf example.tar.gz filename1 filename2

The above command doesn’t show any output on success. So if you want verify that the archive is created or not, then list the directory contents with ls command.

  • If you wish to create the tar.gz inside a directory, then type full path to the archive file same as below:
$ tar -czf /home/user/example.tar.gz filename1 filename2
  • Create a Tar File named “wordpress_backup.tar.gz” of the /var/www/wordpress directory:
$ tar -czf wordpress_backup.tar.gz /var/www/wordpress

More Examples :

  • Create a tar.gz file with all “.txt” files using wildcard (*):
$ tar -czf text.tar.gz *.txt
  • Create a tar.gz file and transfer and extract it on the remote server:
$ tar cvf - project_remote | ssh username@ip_address "tar xv -C /var/www"
  • A system with older version of tar that doesn’t support compression method then you can use the gzip command:
tar -czf - filename1 filename2 | gzip > example.tar.gz

That’s all

If you find any error and issue in above steps , please use comment box below to report.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top