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.
Tutorial requirements
Operating system/appLinux
Root privileges required No
Difficulty Easy (rss)
Estimated completion time 5m
Table of contents


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 and options 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
# Create a tar.gz file from all pdf (".pdf") files
tar -czvf archive.tgz *.pdf

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

Linux list tar.gz file contents

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

Getting help

Type the following man command:
$ man tar
$ tar --help


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.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

4 comments… add one
  • Master Po Feb 3, 2014 @ 6:37

    I create a .tar.gz archive using command line:
    tar -czf file.tar.gz

    I extract a .tar.gz archive using command line:
    tar -xzf file.tar.gz

  • Tina Jul 13, 2015 @ 12:43

    I search for “how to tar a file in linux” in Google and this page helped me. FYI, x is for extract and c is for create.

    • Raj Jul 23, 2020 @ 7:44

      We can use ‘.tgz’ extension instead of ‘.tar.gz’ to save long file names
      # create tgz backup file
      tar zcf /backups/docs.tgz $HOME/Documents/
      # Extract tgz backup file
      tar /backups/docs/zxf backup.tgz -C $HOME/Documents/

  • Nitiratna Nikalje Jul 23, 2020 @ 14:59

    Multiple ways
    1) .bz2 This will create tarball bigtarball.tar.bz2 containing filename1/2 and directory1

    # tar -jcvf bigtarball.tar bz2 filename1 filename2 directory1

    # tar -jxvf bigtarball.tar.bz2
    See content without extracting:-
    # tar -jtvf bigtarball.tar.bz2

    This wont impact existing file

    2) .gz This is ideal for compressing single file
    # gzip filename

    # gunzip filename.gz
    See content without extracting
    # gunzip -l filename.gz

    gzip will create filename.gz tarball and will remove orig file.

    (tip) before extracting tarball make sure no existing file/dir has same name.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.