Uses of Tar Command in Unix and Linux Server

February 27, 2012 / Linux

Uses of Tar Command in Unix and Linux Server :

For archiving purposes on the Unix platform, tar command is the command to be used. Knowing the various tar command options will help you to be a master of archive file manipulation.

A. Create an archive with the tar command

* Create an uncompressed tar archive with option cvf

It is the most basic command to create a tar archive.

$ tar cvf archive_name.tar dirname/
Note :
c – create a new archive
v – verbosely list files which are processed.
f – following is the archive file name

* Create a tar gzipped archive with option cvzf

The above used tar cvf, can’t offer any compression. So, if you want to utilize a gzip compression on the tar archive then you can utilize the z option as follows.

$ tar cvzf archive_name.tar.gz dirname/
Note :
z – filter the archive through gzip

Note:

.tgz is similar as .tar.gz

* Create a bzipped tar archive with option cvjf

Create a bzip2 tar archive as shown below:

$ tar cvfj archive_name.tar.bz2 dirname/
Note :
j – filter the archive through bzip2

Note:

.tbz and .tb2 is similar as .tar.bz2

B. Extracting (untar) an archive with the tar command

* Extract a *.tar file with option xvf

Extract a tar file with options x as shown below:

$ tar xvf archive_name.tar
Note :
x – extract files from archive

* Extract a gzipped tar archive ( *.tar.gz ) with option xvzf

Use the option z for uncompressing a gzip tar archive.

$ tar xvfz archive_name.tar.gz

* Extract a bzipped tar archive ( *.tar.bz2 ) with option xvjf

Use the option j for uncompressing a bzip2 tar archive.

$ tar xvfj archive_name.tar.bz2

C. Listing an archive with tar command

* View the tar archive file content without extracting with option tvf

You can find out the *.tar file content before extracting as shown below.

$ tar tvf archive_name.tar

* View the *.tar.gz file content without extracting with option tvzf

You can find out the *.tar.gz file content before extracting as shown below.

$ tar tvfz archive_name.tar.gz

* View the *.tar.bz2 file content without extracting using option tvjf

You can find out the *.tar.bz2 file content before extracting as shown below.

$ tar tvfj archive_name.tar.bz2

D. Process To Add a file or directory to an existing archive With option -r

You can add additional files to an existing tar archive as shown below. Like, to append a file to *.tar file do the following:

$ tar rvf archive_name.tar updatedfile

This updated file will be added to the existing archive_name.tar. Adding a directory to the tar is also similar,

$ tar rvf archive_name.tar newdir/

Note:

You can’t add a file or directory to a compressed archive. In that case, you will get the following error

$ tar rvfz archive_name.tgz newfile
tar: Cannot update compressed archives
Try `tar --help' or `tar --usage' for more information.

E. Way To Estimate The tar Archive Size

Before creating the tar file you can estimate the tar file size ( in KB ) with the command below:

$ tar -cf - /directory/to/archive/ | wc -c
20480

Before creating the tar.gz, tar.bz2 files, you can estimates the compressed tar file size ( in KB ) with the commands below:

$ tar -czf – /directory/to/archive/ | wc -c 508 $ tar -cjf – /directory/to/archive/ | wc -c 428

Leave a Reply

Your email address will not be published. Required fields are marked *