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