How to create tar.gz file in Linux using command line

I am a new Linux user. How do I create tar.gz file in Linux operating systems using the command line option?

Introduction: A tarball (or tar.gz or tar.bz2) is nothing but a computer file format that combines and compresses multiple files. Tarballs are common file format on a Linux or Unix-like operating systems. Tarballs are often used for distribution of software/media or backup purposes. This page shows how to create tar.gz file in Linux using the Linux command line.

Say hello tar command

You need to use the tar command. The tar command first offered in the seventh edition of unix v7 in January 1979. Each tarball stores data along with additional information such as:

  • File names
  • Directory structure
  • File permission
  • SELinux permissions
  • Timestamps
  • File ownership
  • File access permissions and more

How to create tar.gz file in Linux using command line

The procedure to create a tar.gz file on Linux is as follows:

  1. Open the terminal application in Linux
  2. Run tar command to create an archived named file.tar.gz for given directory name by running: tar -czvf file.tar.gz directory
  3. Verify tar.gz file using the ls command and tar command

Let us see all commands in details.

Linux create tar.gz file with tar command

Say you want to create tar.gz file named projects.tar.gz for directory called $HOME/projects/:
$ ls -l $HOME/projects/
Sample outputs:

total 8
drwxr-xr-x 2 vivek vivek 4096 Oct 30 22:30 helloworld
drwxr-xr-x 4 vivek vivek 4096 Oct 30 13:16 myhellowebapp

The syntax for the tar command is as follows:
tar -czvf filename.tar.gz /path/to/dir1
tar -czvf filename.tar.gz /path/to/dir1 dir2 file1 file2

To create projects.tar.gz in the current working directory, run:
$ tar -czvf projects.tar.gz $HOME/projects/


Next verify newly created tar.gz file in Linux using the ls command:
ls -l projects.tar.gz
Example outputs:

-rw-r--r-- 1 vivek vivek 59262 Nov  3 11:08 projects.tar.gz

One can list the contents of a tar or tar.gz file using the tar command:
$ tar -ztvf projects.tar.gz

Click to enlarge image

How to extract a tar.gz compressed archive on Linux

The syntax is follows to extract tar.gz file on Linux operating system:
$ tar -xvf file.tar.gz
$ tar -xzvf file.tar.gz
$ tar -xzvf projects.tar.gz

One can extract files in a given directory such as /tmp/:
$ tar -xzvf projects.tar.gz -C /tmp/
Sample outputs:


Understanding tar command options

tar command option Description
-c Create a new archive
-x Extract files from an archive
-t List the contents of an archive
-v Verbose output
-f file.tar.gz Use archive file
-C DIR Change to DIR before performing any operations
-z Filter the archive through gzip i.e. compress or decompress archive


You learned how to create a new tar.gz archive, view and extract given archive file using tar command. For more info see GNU/tar help page here.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.