Tar Command Examples :
In my previous article, I have given different Unix commands with real-world examples. In this article, I would like to give you Tar command examples with its explanation. I would like to give you real-world Tar Command Examples so
Tar Command Examples :
In my previous article, I have given different Unix commands with real-world examples. In this article, I would like to give you Tar command examples with its explanation. I would like to give you real-world Tar Command Examples so that user will get exact idea about the command. The Tar command stands for Tape Archive command. Tar command is the most used command in Linux to take tape drive backup in Unix or Linux.
It is specifically used for creating, maintaining and modifying and extracting the files which are there in tar format.It gives you to quickly access a collection of files and placed them into a highly compressed archive file commonly called tarball, or tar, gzip, and bzip in Linux.
Tar Command Examples and Syntax :
In this section, I will try to explain the syntax of Tar command in Unix. I would like to give you different Tar Command Examples which gives the idea about Tar command and its usages.Tar command is mainly used to create compressed or uncompressed archive files and maintain them.
tar [Tar_options] [tar_archive-file] [diiferent files or directory to be archived];
I would like to explain the above syntax.
Tar Command uses different options, Before explaining Tar Command Examples I want to give you different options which are used in Tar Command.
-c: This option creates an archive
-x: This option extracts the archive
-f: This option creates the archive as per given filename in command.
-t: This option displays or lists files in the archived file
-u: This option archives and adds to an existing archive file
-v: This option displays verbose information
-A: This option concatenates the archive files
-z: If the user wants to create tar file using gzip this option is used.
-j: This option filters archive tar file using tbzip
-W: This option verifies an archive file
-r: This option update or add file or directory in the already existed.tar file
About tar_archieve-file :
An Archive file is a file that is composed of one or more files along with metadata. Archive files are used to collect multiple data files together into a single file for easier portability and storage, or simply to compress files to use less storage space.
file or directory to be archived :
The name of the file or name of the directory that needs to be archived.
The following are some important Tar Command Examples which will give the user the idea about real life use of Tar Command Examples :
Example 1: Creating the Tar Archive using the option CVF
In the above section, I have explained many Tar command options. The Tar commands CVF option is a combination of options given in the above paragraph. The option c will create the archive, the option v will verbose the information and option f will create a file.
$ tar -cvf complexsql.tar.*java
The above command will create the complexsql.tar file which has all.java files in the specified directory.
In above example following options are used for the specific purpose:
-c Option: This option creates the new tar archive file which contains all.java files from a directory.
-v Option: This command is used to create Verbose information for tar file.
-F Option: This command is used to give the new filename to the tar file. Here in the example, we have given the name as complexsql.tar.
After creating the new tar file named complexsql.tar we need to check whether it is created properly or not.
$ls -l complexsql.tar
The output of a given command is as follows.
-rw-r–r–. 1 root Amit 14744441 Aug 29 00:24 complexsql.tar
Example 2: Creating the tar file with using the bunch of files
If the user knows the file names which needs to be included in tar file then always it is better to use the different options provided by tar command like –file and –create. Let’s say the user wants to create tar file for 2 files named Complexsql1.txt and Complexsql2.txt. The user can achieve this using Tar command with its different options. Following are some examples of tar command with different options.
Example 2.1: Create the complexsql.tar file using –file and –create options
tar –create –file=complexsql.tar Complexsql1.txt Complexsql2.txt
Example 2.2: Create the complexsql.tar file using -c and -f options
tar -c -f complexsql.tar Complexsql1.txt Complexsql2.txt
Example 2.3: Create the complexsql.tar file using -cf
tar -cf complexsql.tar Complexsql1.txt Complexsql2.txt
These all commands will create the complexsql.tar file which contains the two text files Complexsql1.txt and Complexsql2.txt.
Example 3: Display the list of contents of the tar file
If the user wants to display the list of contents of tar file without extracting it user needs to use the -tvf option of tar command.
-t : This option displays or lists files in archived fil