Categories: Unix Tutorials

Tar Command Examples | What is Tar Command in Unix with Examples?

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.

Syntax :

tar [Tar_options] [tar_archive-file] [diiferent files or directory to be archived];

I would like to explain the above syntax.

Tar_options :

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 file

$tar -tvf complexsql.tar root/amit/

The above command will show the list of files in complexsql.tar command with its verbose information. The output of the same is :

-rw-r–r–. 1 root Amit 14744441 Aug 29 00:24 complexsql.tar complexsql1.txt complexsql2.txt

Example 4: Compressing Entire directory using Tar command and gzip

The user can zip the entire directory using -z option of Tar command. There are so many requirements where the user needs to send the entire directory. To send the entire directory user needs to create the zip file of that directory using gzip. Let’s say we need to create the zip file using gzip and tar command for the complexsql directory.

tar -czvf complexsql.tar.gz complexsql

The above command will create the complexsql.tar.gz using gzip directory.

If the user is not working in the complexsql directory and it has some path. The user wants to create a tar file using gzip for complexsql directory situated in /etc/bin/complexsql path. The the following command is useful.

tar -czvf complexsql.tar.gz /etc/bin/complexsql

The above command will create a complexsql.tar.gz file which contains different files from a directory named /etc/bin/complexsql.

Example 5: Adding new files in the tar file

Sometimes there the situations where the user needs to add new files from different directories in the tar file. -r option of tar command is used to add new files in the tar file. In our example, let’s say we need to add the complexsql3.txt file.

$tar -rvf complexsql.tar /etc/bin/complexsql/complexsql3.txt

The above command will add the complexsql3.txt file to complexsql.tar file.-r the option is responsible to add the new files or append the new files in tar.

Things to Remember: We can not add the new file in compressed tar file which we have created using gzip in Example no.4.

Example 6: Excluding some directories or file while creating a tar file

We need to exclude some files or some directories while creating the tar file. –exclude option is used to exclude the bunch of files or directories while creating the tar file. Let’s say a user wants to exclude the file named complexsql3.txt while creating the tar file.

$tar -czvf complexsql.tar.gz /etc/bin/complexsql/ –exclude /etc/bin/complexsql/complexsql3.txt

The above command will create the zip file for the complexsql directory which excludes complexsql3.txt file.

Example 7: Extracting file and directories using the tar command

After creating the tar file or compressing the tar file we need to extract that tar file. There are some situations where the user needs to extract the tar files. -x option of tar command extracts the files as well as directories from the specified tar file.

$tar -xvf complexsql.tar

The above command will extract the files from complexsql.tar.

Example 8: Extracting files and directories and place it in a particular folder

Sometimes a user needs to extract the files from tar file and need to place it to the specific folder. -C option of tar command will copy all the content in the tar file and will place it to the specified folder.

$tar -xvf complexsql.tar -C /complexsql/

The above command extracts all the files and folders from the complexsql.tar file and the data placed in complexsql folder.

Example 9: Extracting the specific file from tar 

Sometimes the user wants some specific files from the tar file. The user can extract specific file or directory from tar file with using its path.

Syntax :

$tar –xvf [tar-file-name] [file-wanted-to-be-extract] -C [path-where-to-extract]

Example :

$tar -xvf /etc/bin/complexsql/complexsql3.txt -C /BestWebsite/

The above command will copy the file name complexsql.txt from /etc/bin/complexsql/ path and paste it to /BestWebsite/ directory. -C option use for the copying of the files and pasting it into the BestWebsite folder.

Example 9: Creating bz2 tar compressing archive file using Tar command

There is the difference between gzip file and bz2 files. The bz2 files are less in size as compare to gzip files. Tar command provides the option to create bz2 files.bz2 compression takes more time than the compression of gz but it saves the memory.Tar command’s -j option is used to create bz2 files.

$tar -jcpvf complexsql.tar.bz2 /etc/bin/complexsql/

The above command will create the complexsql.tar.bz2 file from the complexsql directory.

Example 10: List the contents of Tar.bz2 and .tbz2 files

The user can able to see the content from bz2 or tbz2 files without decompressing it. The decompression of bz2 and tbz2 file takes time. Using -t option of tar command user can able to check the contents of the file.

$tar -tvf complexsql.tar.bz2 

The above command will list the contents of complexsql.tar.bz2 file.

Example 11: Extracting the bz2 and tbz2 files using -j option

Once we create the bz2 and tbz2 files we need to extract the files into a specific folder. To extract the files form bz2 or tbz2 tar command has provided the option called -j. The user needs to use -x option to extract the tar files and -j option to check for bz2 or tbz2 files.

$tar -jxpvf complexsql.tar.bz2 

The above command will extract the complexsql.tar.bz2 in the current working directory.

Example 12: Backup scheduling using the tar command

There are so many times the user needs to schedule the backup. The user wants to schedule the backup to the particular time on daily, weekly or any time intervals. The Crone job takes the backup. The creating crone jobs are the only solution to schedule the set of commands in Unix operating system.

Let’s say if we need to take backup of the complexsql folder on a daily basis user need to use the following command and for this command, we need to create crone job.

tar -zcvf complexbackup-$(date +%Y-%m-%d).tgz /complexsql/

The above command will create a backup of the complexsql folder on daily basis.

Example 13: Search something by using the pipe operator and Grep command in tar:

If User wants to find out some text or some image from tar then following commands are useful. The user can achieve this using the Pipe operator with Grep command.

Syntax:

$ tar tvf Filename.tar | grep “Text needs to find out”

$ tar tvf Filename.tar | grep “File.extension need to find out”

Example :

$ tar tvf complexsql.tar | grep “Complexsql is the best website for learning SQL”

$ tar tvf complexsql.tar | grep “Complexsql.jpeg”

The above example used find out the text as well as image files from tar file named complexsql.tar.

. If you really like this article or if you have some questions or concerns regarding Tar Command Examples article kindly comment it in to comment section. I hope you the Tar Command Examples article is useful to everyone.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Share
Published by
Amit S

Recent Posts

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

2 days ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

5 days ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

5 days ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

5 days ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

5 days ago

What are Roles and Responsibilities of L1 Support Engineer with real industry examples?

In this series we are starting with Roles and responsibilities of L1 support engineer .…

5 days ago