Create tar.gz file in Linux command line

In this tutorial we are going to teach you to create tar.gz file in Linux command line.

Create tar.gz file in Linux command line

How to create tar.gz file in Linux?

In this tutorial I will show you how to create tar.gz file in Linux? We are going to use the Linux console and then run the commands to create tar.gz file that will achieve content of directory(directories and sub directories) or simply one file. You can provide the input directory name or file name for adding into the tar.gz file.

Linux tar command - How to create a tar.gz file in Linux to archive a directory and all the content inside that directory including all subdirectories and files?

The term tar stands for tape archive which is used in Linux/Unix for taking the tape backup. This command is used by the administrator of Linux/Unix system. The tar utility was developed at AT&T Bell Lab and initial release was released in January 1979, so this utility is very old in Linux. This utility evolved with the Linux/Unix system and now it is one of the most used utility in Linux.

This utility is used to create the archive backup of the files, directory and directories including all subdirectories. This utility is most commonly used utility by users and administrator of Linux/Unix system. There is some software available in Windows operating system which can be used to create the tar.gz archive on windows system as well. But this utility is mostly used with the Linux, UNIX and Mac operating systems.

What is tar utility in Linux/UNIX?

Tar is utility in Linux/UNIX which comes with the Linux Operating System and it can  be accessed in command line mode in Linux terminal. Open the terminal in Linux and type 'tar --help' it will display the various options of using the tar command as shown below:


tar --helpdeepak@deepak-VirtualBox:~$ tar --help
Usage: tar [OPTION...] [FILE]...
GNU 'tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.

Examples:
  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
  tar -tvf archive.tar         # List all files in archive.tar verbosely.
  tar -xf archive.tar          # Extract all files from archive.tar.

 Local file name selection:

      --add-file=FILE        add given FILE to the archive (useful if its name
                             starts with a dash)
  -C, --directory=DIR        change to directory DIR
      --exclude=PATTERN      exclude files, given as a PATTERN
      --exclude-backups      exclude backup and lock files
      --exclude-caches       exclude contents of directories containing
                             CACHEDIR.TAG, except for the tag file itself

Here is the screen shot taken on the Ubuntu 18.04 Operating System:

Linux Tar command Help

Syntax of Linux tar command

Let's see the syntax of tar command:


tar [options] [archive-file-name] [file(s) or directory to be archived]

You can give following options with the Linux tar command:

-c : For creating Archive file
-x : For extracting an archive file
-f : This option is used to create an archive with given filename
-t : Use this option for displaying or listing files in archived file
-u : You can use this option to adds files to an existing archive file
-v : Displays Verbose Information
-A : This option is used when you have requirements to concatenate the archive files
-z : zip, tells tar command that create tar file using gzip
-j : filter archive tar file using tbzip
-W : Verify a archive file
-r : update or add file or directory in already existed .tar file

So, with the tar command you can use these options.

Example of creating tar.gz file in Linux

There are many ways you can use the tar command to create tar.gz file on Linux system.

Creating tar.gz of a single file

Suppose you have a file called "first_file.txt" then you can use the following command to create the tar.gz file for compressing "first_file.txt":


tar cvzf first_file.tar.gz first_file.txt

Here is the screen shot of the steps:

Creating tar.gz file of a directory (including all files and subdirectories)

Now we will show you how you can create a tar.gz file (archive) of a directory that includes the content in all sub-directories. This command will recursively check files and directories and include in the gar.gz file. This command is useful if you have large number of files, directories and subdirectories and you have to create archive of all the data preserving the directory structure.

Suppose you have a directory "mydata" containing files and directories inside it then you can use the following command:


tar cvzf mydata.tar.gz mydata

Here is the output of the command:

So, we have learned how to create the tar.gz archive file from a directory which includes all the contents inside the directory.

How to extract tar.gz file?

Now we will finally see how to extract or un archive a tar.gz file. In the above example we have created the mydata.tar.gz file, for un archiving this file you can use the following command:

In this tutorial you have learned to create tar.gz archive and then extract the tar.gz file. The tar utility is very powerful and handy tool in the hand of Linux administrator for creating archive file for taking backup of the system.

Related tutorials: 0

Tutorials

  1. Beginner Guide to Linux Server
  2. Beginner Guide to Linux Server
  3. Roseindia.net Place to Get Cheap Linux cds in India!!
  4. RoseIndia.net Is Your On-Line Linux & xBSD CD Distribution Source
  5. RoseIndia.net Is Your On-Line Linux & xBSD CD Distribution Source
  6. We are providing Downloadable Version of Dyne:bolic 1.3 Linux
  7. We are providing Fedora Cord 2 Linux CD's .
  8. Buy Fedora Core 3 Linux CD's in India.
  9. Buy Fedora Core 3 Test 1 Linux CD's in India.
  10. We are providing Linux CD's for free.
  11. Free Linux Distribution in India
  12. We are providing Free BSD 5.3 CD's
  13. We are providing Downloadable Version of Gentoo Linux
  14. Linux Tutorials
  15. We are providing Downloadable Version of K12LTSP Linux
  16. We are providing Downloadable Version of K12LTSP Linux
  17. We are providing Knoppix 3.4 Live Linux CD's
  18. We are providing Knoppix 3.6 Live Linux CD's
  19. We are providing Knoppix 3.7 Live Linux CD's
  20. We are providing Linux CD's for free.
  21. Buy KANOTIX BUG HUNTER 09-2004-A Live CDs in India from us. KANOTIX BUG HUNTER 09-2004-A Live is available with us.
  22. We are providing Downloadable Version of Mandrake 10.1 Community Edition Linux CD's.
  23. We are providing Downloadable Version of Mandrake 10.1 Official Edition Linux CD's.
  24. We are providing Downloadable Version of Mandrake 10.1 Power Pack Linux CD's.
  25. We are providing Linux CD's for free.
  26. We are providing Linux CD's for free.
  27. We are providing Downloadable Version of Mandrake Move Linux
  28. We are providing Downloadable Version of Mandrake Official 10 Linux CD's.
  29. TheOpenCD is a collection of high quality Free and Open Source Software
  30. Buy Peanut 9.6 Linux in India from us. Peanut 9.6 distribution is available in India.
  31. We are providing Linux CD's for free.
  32. Buy Slackware 10.0 in India from us. Slackware 10.0 is available with us.
  33. We are providing Downloadable Version of Slackware 9.1 Linux CD's.
  34. Buy SuSe 9.1 Live CDs in India from us. Suse 9.1 Live is available with us.
  35. Buy SuSe 9.1 Personal CDs in India from us. Suse 9.1 Personal is available with us.
  36. Buy SuSe 9.2 Live CDs in India from us. Suse 9.2 Live is available with us.
  37. Buy SuSe 9.2 Professional CDs in India from us. SuSe 9.2 Professional is available with us.
  38. Buy SuSe 9.1 Professional CDs in India from us. Suse 9.1 Professional is available with us.
  39. Buy Xandros 2.01 OCE in India from us.Xandros 2.01 OCE is available in India at cheapest rate.
  40. The Beginners Linux Guide