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

last updated in Categories , , , , , ,

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/
.cyberciti.biz/media/new/faq/2013/06/How-to-create-tar.gz-file-in-Linux-using-command-line.png

Verification

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:

home/vivek/projects/myhellowebapp/
home/vivek/projects/myhellowebapp/hellowebapp.working/
home/vivek/projects/myhellowebapp/hellowebapp.working/db.sqlite3
home/vivek/projects/myhellowebapp/hellowebapp.working/manage.py
home/vivek/projects/myhellowebapp/hellowebapp.working/collection/
home/vivek/projects/myhellowebapp/hellowebapp.working/collection/models.py
home/vivek/projects/myhellowebapp/hellowebapp.working/collection/admin.py
home/vivek/projects/myhellowebapp/hellowebapp.working/collection/__init__.py
home/vivek/projects/myhellowebapp/hellowebapp.working/collection/tests.py
home/vivek/projects/myhellowebapp/hellowebapp.working/collection/static/
home/vivek/projects/myhellowebapp/hellowebapp.working/collection/static/css/
home/vivek/projects/myhellowebapp/hellowebapp.working/collection/static/css/style.css
...
..
...
home/vivek/projects/myhellowebapp/hellowebapp/hellowebapp/__pycache__/urls.cpython-36.pyc
home/vivek/projects/myhellowebapp/hellowebapp/hellowebapp/wsgi.py
home/vivek/projects/myhellowebapp/hellowebapp/hellowebapp/settings.py
home/vivek/projects/helloworld/

Understanding tar command options

tar command optionDescription
-cCreate a new archive
-xExtract files from an archive
-tList the contents of an archive
-vVerbose output
-f file.tar.gzUse archive file
-C DIRChange to DIR before performing any operations
-zFilter the archive through gzip i.e. compress or decompress archive

Conclusion

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: Vivek Gite

The author is the creator of nixCraft 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.

Start the discussion at www.nixcraft.com

Historical Comment Archive

2 comment

  1. 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

  2. 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.

    Have a question? Post it on our forum!