How to Create tar.gz file in Linux

Last updated on August 21st, 2024 at 05:53 am

The tar command in Linux allows you to compress files and directories into tar.gz files, also known as tarballs. They are very useful in data archival, compression and distribution. Here’s how to create tar.gz file in Linux. We will also learn how to extract a given tar.gz file.

How to create tarball in Linux

We will use the tar command to create tar.gz file in Linux. Here’s the syntax

$ tar -cvzf filename.tar.gz /path/to/file [paths to more files/directories]
OR
$ tar -cvzf filename.tar.gz /path/to/directory [paths to more files/directories]

In the above command, the tar command is followed by options -cvzf, the filename of final tar.gz file to be created, followed by one or more paths to files/directories

In the above command we mention the following options

  • c – creation of archive file
  • v – for verbose, meaning it will display the progress in terminal
  • z – gzip the archived .tar file
  • f – filename of archive file

Bonus Read : How to Create Zip and Unzip File in Linux

Please note: Tar command used without -z option will only archive your file and create a .tar file. If you want it to be further compressed using gzip algorithm, you need to include -z option. In this case, it will create .tar.gz file. That is why you see a chained extension .tar.gz or .tar.bz2 in tarballs.

Let us look at a few examples to create tar.gz file

Create Tarball from Single File

Here’s the command to create tar.gz file (e.g sales_data.tar.gz) from a single file (e.g /home/ubuntu/sales_data.txt)

$ tar -cvzf sales_data.tar.gz /home/ubuntu/sales_data.txt

First tar command will create the archive .tar file and then compress it to create .tar.gz file. That is why you see 2 file extensions in a tarball.

Bonus Read : How to Find Directory in Linux

Tar Multiple Files

If you want to create a single tar.gz file from multiple files, just mention them one after the other in a space separated format, at the end of tar command as shown below.

$ tar -cvzf sales_data.tar.gz sales1.txt sales2.txt sales3.txt

How to create a tar.gz file from a directory

Here’s the tar command to create tar.gz file from a directory (e.g /home/ubuntu/product/)

$ tar -cvzf product.tar.gz /home/ubuntu/product/
/home/ubuntu/product/
/home/ubuntu/product/file1.txt
/home/ubuntu/product/file2.exe
/home/ubuntu/product/file3.pdf

tar command will list contents of your directory that is being archived. The tar file will be created in present working directory.

If you want to create the tar.gz file in a specific directory, mention the full path to tar.gz file.

$ tar -cvzf /home/data/product.tar.gz /home/ubuntu/product/
/home/ubuntu/product/
/home/ubuntu/product/file1.txt
/home/ubuntu/product/file2.exe
/home/ubuntu/product/file3.pdf

Bonus Read : How to List all files in a Directory in Linux

Create tar.gz from multiple folders

You can even create tar file from multiple directories by mentioning them one after the other in a space-delimited format as shown.

$ tar -cvzf /home/data/product.tar.gz /home/ubuntu/folder1/ /home/ubuntu/folder2/
/home/ubuntu/product/
/home/ubuntu/product/file1.txt
/home/ubuntu/product/file2.exe
/home/ubuntu/product/file3.pdf
...

Create tarball from files and folders

You can also compress files and folders together by listing them one after the other in a space separated format.

$ tar -cvzf /home/data/product.tar.gz /home/ubuntu/folder1/ /home/ubuntu/data.txt
/home/ubuntu/product/
/home/ubuntu/product/file1.txt
/home/ubuntu/product/file2.exe
/home/ubuntu/product/file3.pdf
...

How to Extract tar.gz file to current directory

Here’s the command to extract tar.gz file to current working directory

$ tar -xvf product.tar.gz
/home/ubuntu/product/
/home/ubuntu/product/file1.txt
/home/ubuntu/product/file2.exe
/home/ubuntu/product/file3.pdf

tar command will list the contents of your tar.gz file that is being extracted.

In the above command, we use -x option for extraction, instead of using -c for compression. Other options are same as above. You do not need to use it if you have only archived the file and created .tar file without compressing it into .tar.gz file.

Extract tar.gz file to specific directory

Here’s the command to extract files to a specific directory (e.g /home/ubuntu/data). You need to mention full path to destination directory after tar.gz command.

$ tar -xvf filename.tar.gz /path/to/dir
$ tar -xvf product.tar.gz /home/ubuntu/data

Conclusion

In this article, we have learnt how to create tar.gz file from files and folders, in current directory as well as another directory. We have also learnt how to extract tar.gz file. Hopefully, the above tutorial will help you create tarball as well as extract tar.gz files in Linux.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!