Saturday, November 5, 2011

10 Examples of tar command in UNIX and Linux

tar command in UNIX or Linux is one of the important command which provides archiving functionality in unix. we can use UNIX tar command to create compressed or uncompressed archive files by using either gzip or bzip2. In this unix tar command tutorial we will see examples of unix tar command related to basic archiving task e.g. How to create tar archive in Unix and Linux, How to extract files from tar archive in unix, How to view contents of tar file in Unix and Linux or how to update and existing tar file in Unix. Examples of tar command in unix are kept simple and easy to understand and master each of basic task using unix tar command.

I thought about this article when I written how to be productive in UNIX and UNIX command tutorial and Example for beginners but somehow it gets delayed and now I am happy to see this published. 
Ok enough introduction now let's see some real life examples of tar command in Unix and Linux:

tar command in unix linux examples

How to use tar command in Unix

Using tar command in UNIX is simple and it has similar syntax like any other UNIX command. below is the syntax of tar command in UNIX:

tar  [options] [name of tar file to be created] [list of files and directories to be included]

This syntax of tar command is for easy understanding you can also check detailed syntax by using command "tar --usage" in unix machine.

tar command examples in Linux


Unix tar command line options
---------------------------------------
In this section of UNIX tar command tutorial we will see some useful options of tar command in Linux and we will use this options on our example to understand usage of this option along-with tar command.

c -- create, for creating tar file
v -- verbose, display name of files including,excluding from tar command
f -- following, used to point name of tar file to be created. it actually tells tar command that name of the file is "next" letter just after options.

x -- extract, for extracting files from tar file.
t -- for viewing content of tar file
z -- zip, tells tar command that create tar file using gzip.
j –- another compressing option tells tar command to use bzip2 for compression
r -- update or add file or directory in already existed .tar file
wildcards -- to specify patters in unix tar command


How to create tar archive or tar file in Unix
-------------------------------------------------------
Most of use use either winzip or winrar in windows machine to zipping or creating archives of content so when we move to command line interface like Unix or Linux we struggle without those tools. UNIX tar command is similar to winzip or winrar and you can use UNIX tar command to create both compressed or uncompressed (zipped) archives in UNIX.

In this example of tar command we will create tar file including all the files and directories or selected files and directories in Unix.

here is our directory

stock_trader@system:~/test ls -lrt
total 0
-r--r--r--  1 stock_trader Domain Users 0 Jul 15 11:42 equity
drwxrwxrwx+ 1 stock_trader Domain Users 0 Jul 15 14:33 stocks/
-r--r--r--  1 stock_trader Domain Users 0 Jul 15 15:30 currency

it has two files and one directory. now we will create a tar file with all these contents.

stock_trader@system:~/test tar -cvf trading.tar *
currency
equity
stocks/
stocks/online_stock_exchanges.txt

You see unix tar command is creating tar file with name "trading" with contents shown above. just to review here "-c" is used to create tar file "v" is used to be verbose and "f" is used to tell tar file name. You can see the tar file here

stock_trader@system:~/test ls -lrt
-r--r--r--  1 stock_trader Domain Users   0 Jul 15 11:42 equity
drwxrwxrwx+ 1 stock_trader Domain Users   0 Jul 15 14:33 stocks/
-r--r--r--  1 stock_trader Domain Users   0 Jul 15 15:30 currency
-rw-r--r--  1 stock_trader Domain Users 10K Jul 18 12:29 trading.tar


How to view contents of tar file in Unix or Linux
-------------------------------------------------------------
In earlier example of tar command in Unix or Linux we have created a uncompressed tar file called "trading.tar" now in this example we will see the actual content of that tar file.

stock_trader@system:~/test tar -tvf trading.tar
-r--r--r-- stock_trader/Domain Users 0 2011-07-15 15:30 currency
-r--r--r-- stock_trader/Domain Users 0 2011-07-15 11:42 equity
drwxrwxrwx stock_trader/Domain Users 0 2011-07-15 14:33 stocks/
-rwxrwxrwx stock_trader/Domain Users 0 2011-07-15 14:33 stocks/online_stock_exchanges.txt

here option "t" is used to display content of tar file in unix while options "v" and "f" are for "verbose" and "following". now you can clearly see that all the files which we wanted to be included in tar file are there.


How to extract contents from a tar file in Unix
-----------------------------------------------------------
In this example of unix tar command we will see how to extract files or directories from a tar file in unix or Linux. We will use same trading.tar file created in earlier example. In this example we will create a directory "trading" and extract contents of trading.tar on that directory.

stock_trader@system:~/test/new ls -lrt
total 12K
-rw-r--r-- 1 stock_trader Domain Users 10K Jul 18 12:37 trading.tar

Now the directory is empty just trading.tar file

stock_trader@system:~/test/new tar -xvf trading.tar
currency
equity
stocks/
stocks/online_stock_exchanges.txt

This unix tar command will extract content of trading.tar in current directory. "x" is used for extracting. "v" is again for verbose and optional parameter in all our example.

stock_trader@system:~/test/new ls -lrt
-r--r--r--  1 stock_trader Domain Users   0 Jul 15 11:42 equity
drwxr-xr-x+ 1 stock_trader Domain Users   0 Jul 15 14:33 stocks/
-r--r--r--  1 stock_trader Domain Users   0 Jul 15 15:30 currency
-rw-r--r--  1 stock_trader Domain Users 10K Jul 18 12:37 trading.tar

Now you can see that all the files and directories which were included in tar file (stocks, equity and currency) has been extracted successfully.


How to create tar file in Unix with just specified contents
-------------------------------------------------------------------------
In above example of tar command in unix we have created tar file with all the contents available in current directory but we can also create tar file with selective content as shown in above example.

Now in our current directory we have both files and directories and we just want to include two files equity and currency in our tar file.

stock_trader@system:~/test ls -lrt
-r--r--r--  1 stock_trader Domain Users   0 Jul 15 11:42 equity
drwxrwxrwx+ 1 stock_trader Domain Users   0 Jul 15 14:33 stocks/
-r--r--r--  1 stock_trader Domain Users   0 Jul 15 15:30 currency
-rw-r--r--  1 stock_trader Domain Users 10K Jul 18 12:29 trading.tar
drwxr-xr-x+ 1 stock_trader Domain Users   0 Jul 18 12:46 new/

stock_trader@system:~/test tar -cvf equitytrading.tar equity currency
equity
currency

you see only two files equity and currency are included in our tar file.


How to create compressed tar file using gzip in Unix
------------------------------------------------------------------
In our previous example of Linux tar command we have created uncompressed tar file but most of the time we also need to create compressed tar file using gzip or bzip2. In this example of tar command in Linux we will learn about creating tar file using gzip.

stock_trader@system:~/test tar -zcvf trading.tgz *
currency
equity
stocks/
stocks/online_stock_exchanges.txt

you see creating tar file with gzip is very easy just use "-z" option and it will crate a gzip tar. .tgz or tar.gz extension is used to denote tar file with gzip. size of a compressed tar file is far less than uncompressed one.

stock_trader@system:~/test ls -lrt
-r--r--r--  1 stock_trader Domain Users   0 Jul 15 11:42 equity
drwxrwxrwx+ 1 stock_trader Domain Users   0 Jul 15 14:33 stocks/
-r--r--r--  1 stock_trader Domain Users   0 Jul 15 15:30 currency
-rw-r--r--  1 stock_trader Domain Users 219 Jul 18 13:01 trading.tgz

you can also view contents of gzip tar file by using earlier command in combination of "z" option and same is true for extracting content from gzip tar. below examples of unix tar command will show how to view contents of .tgz or .tar.gz file in unix.

stock_trader@system:~/test tar -ztvf trading.tgz
-r--r--r-- stock_trader/Domain Users 0 2011-07-15 15:30 currency
-r--r--r-- stock_trader/Domain Users 0 2011-07-15 11:42 equity
drwxrwxrwx stock_trader/Domain Users 0 2011-07-15 14:33 stocks/
-rwxrwxrwx stock_trader/Domain Users 0 2011-07-15 14:33 stocks/online_stock_exchanges.txt

Similarly we can extract contents from a .tgz or .tar.gz file as shown in below example of unix tar command :

stock_trader@system:~/test/new tar -zxvf trading.tgz
currency
equity
stocks/
stocks/online_stock_exchanges.txt

stock_trader@system:~/test/new ls -lrt
-r--r--r--  1 stock_trader Domain Users   0 Jul 15 11:42 equity
drwxr-xr-x+ 1 stock_trader Domain Users   0 Jul 15 14:33 stocks/
-r--r--r--  1 stock_trader Domain Users   0 Jul 15 15:30 currency
-rw-r--r--  1 stock_trader Domain Users 219 Jul 18 13:07 trading.tgz


How to create compressed tar file using bzip2 in Unix
--------------------------------------------------------------------
bzip2 is another compression option we have which we can use with unix tar command. its exactly similar with our earlier option of compressing using gzip but instead of "z" option we need to use "j" tar option to create bzip2 file as shown in below example of tar command in unix.

stock_trader@system:~/test tar -jcvf trading.tar.bz2 *
currency
equity
stocks/
stocks/online_stock_exchanges.txt

stock_trader@system:~/test ls -lrt trading.tar.bz2
-rw-r--r--  1 stock_trader Domain Users 593 Jul 18 13:11 trading.tar.bz2

.tar.bz2 is used to denote a tar file with bzip2 compression. for viewing contents of bzip2 tar file and extracting content we can use as shown in example of UNIX tar command with gzip compression, just replace "-z" with "-j" for bzip2.



How to extract a particular file form .tar, .tar.gz or .tar.bzip2
----------------------------------------------------------------------------
In previous examples of extracting contetns from tar file we have extracted everything. sometime we just need a specific file from tar file. in this example of unix tar command we will extract a particular file from a tar archive.

stock_trader@system:~/test/new tar -jxvf trading.tar.bz2 equity
equity

its simple just specify name of file in this case its "equity". if your tar file is gzip one then use "-z" that's it. You can also use combination of grep and find command with tar to get more dynamic use.



How to extract group of file or directory from form .tar, .tar.gz or .tar.bzip2 in UNIX
---------------------------------------------------------------------------------------------------
you can extract a group of file form .tar, .tar.gz or .tar.bzip2 in Unix by specifying a matching pattern and using option "--wildcards". let's an example of tar command in unix with --wildcards

stock_trader@system:~/test/new tar -jxvf trading.tar.bz2 --wildcards "s*"
stocks/
stocks/online_stock_exchanges.txt

In above example of UNIX tar command we are extracting all files or directory which names starts with "s".

How to update existing tar file in Linux
----------------------------------------------
You can also update or append new files in already created tar file. option"-r" is used for that. Let’s see an example of updatating tar file using tar command in UNIX:

stock_trader@system:~/test tar -cvf sample.tar equity currency
equity
currency

stock_trader@system:~/test tar -rvf sample.tar gold
gold

stock_trader@system:~/test tar -tvf sample.tar
-r--r--r-- stock_trader/Domain Users 0 2011-07-15 11:42 equity
-r--r--r-- stock_trader/Domain Users 221 2011-07-18 13:10 currency
-rw-r--r-- stock_trader/Domain Users   0 2011-07-18 13:30 gold

Apparently can not update compressed archives.if you try to do you will get error "tar: Cannot update compressed archives"

Calculating size of tar file in UNIX
-------------------------------------------
Some time its useful to know the size of tar file before creating it and you can get it by using unix tar command as shown in below example:

stock_trader@system:~/test tar -cf - * | wc -c
20480

Size shown here is in KB and you can also calculate size for compressed tar file by using "z" for gzip and "j" for bzip2

That's all on this series of 10 example of tar command in UNIX or Linux. If you guys have some other good example of UNIX tar command then please share with us via commenting.



Related UNIX Command Tutorials and Tips

6 comments :

Anonymous said...

Hi, will these example of tar command in unix will also applied on other flavor of unix e.g. Solaris, AIX and Linux ?

Javin @ unix networking command said...

@Anonymous, Thanks for your comment. I have not tested these command on other Unix Operation system e.g. Solaris, AIX or anything else, though I have tested these on Linux and as such these are general tar command examples until you have gnu tar these examples should work properly.

Anonymous said...

Two recommendations:

1) Your blog is very narrow, which makes it hard to read. Try a width of 685 instead of 485.

2) In your introduction, don't just briefly explain what the tar command is. Also tell me why your post is better than just reading "man tar". (For example, you might say "The tar manual page has a huge number of options most people never use. I will briefly explain the 10 most common usages of tar")

Anonymous said...

i have one more doubt ..how to extract multiple tar file in single commands..

Raja Ram said...

tar command is mainly used for zip and unzip archives but that is not the only command available in any Unix operating system. you can use gzip or gunzip for creating and extracting archives and there are couple of more as well.

tar said...

well you can not use tar -zcvf command in AIX. it doesn't support compression directory using -z option. alternatively you can pass output of tar command to gzip for compression like
tar -cvf file | gzip > file.tar.gz

Post a Comment