Create tar.gz file in Linux command line

Ads
 
Tutorials   
Create tar.gz file in Linux command line Download and Install Ubuntu 19.04 on Oracle VirtualBox How to install clang on Ubunut? How to install GParted Partition Manager? yum clear cache PHP MySQL on Centos 5.x Linux Basics Linux Installation Process Hardware Requirement for Linux. Running from CDs Linux Distributions Linux Vs. Windows Linux in Internet Linux as a Command Line Interface (CLI) Vs. Graphics User Interface (GUI). Utility of Linux Significance of Linux What is Linux? Fedora Core 6 Guide, Installing Fedora Core6 Linux Firewall What is Linux? Installing Sun JDK on Linux The Beginners Linux Guide Buy Xandros 2.01 OCE in India from us.Xandros 2.01 OCE is available in India at cheapest rate. Buy SuSe 9.1 Professional CDs in India from us. Suse 9.1 Professional is available with us. Buy SuSe 9.2 Professional CDs in India from us. SuSe 9.2 Professional is available with us. Buy SuSe 9.2 Live CDs in India from us. Suse 9.2 Live is available with us. Buy SuSe 9.1 Personal CDs in India from us. Suse 9.1 Personal is available with us. Buy SuSe 9.1 Live CDs in India from us. Suse 9.1 Live is available with us. We are providing Downloadable Version of Slackware 9.1 Linux CD's. Buy Slackware 10.0 in India from us. Slackware 10.0 is available with us. We are providing Linux CD's for free. Buy Peanut 9.6 Linux in India from us. Peanut 9.6 distribution is available in India. TheOpenCD is a collection of high quality Free and Open Source Software We are providing Downloadable Version of Mandrake Official 10 Linux CD's. We are providing Downloadable Version of Mandrake Move Linux We are providing Linux CD's for free. We are providing Linux CD's for free. We are providing Downloadable Version of Mandrake 10.1 Power Pack Linux CD's. We are providing Downloadable Version of Mandrake 10.1 Official Edition Linux CD's. We are providing Downloadable Version of Mandrake 10.1 Community Edition Linux CD's.

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.

In this tutorial we are going to teach you to 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 [email protected]:~$ 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:

Ads

Ads