01 March 2012

tar Command in Linux and Unix

Leave a Comment

how to compress file in linux using tar command
tar command in Linux/Unix used to create archive on floppy , hard disk or tape. tar command does not delete original files or directories. Lets do some example to understand the functionality of tar command

1. Create an archive using tar command

$ sudo tar -cvfp tar_file.tar directory_name

In the above command

tar : command name
c: create or overwrite archive
v: verbose
f: file name
p: preserve permission
tar_file.tar: Archive name
directory_name: directory to be archived

Example

We want to archive entire Linux except /media directory in / directory.

$ sudo tar -cvpf backup.tar --exclude=/media /

2.Create tar gzipped archive

$ sudo tar -cvfpz backup.tar.gz directory_name

z: archive through gzip.
* .tar.gz is same as .tgz.

3.Create tar bzipped archive

$ sudo tar -cvfpj backup.tar.bz2 directory_name

j: archive through bzip2.
* .tar.bz2 is same as .tbz and .tb2.

Difference between gzip and bzip2

bzip2 take more time in compression and decompression  compare to gzip , but size of bzip2 archive is smaller than gzip.

Note: You can use --exclude option with gzip and bzip2 also.

How to Extract files from different tar archives

Syntax

$ sudo tar -options archive_name -C dest_folder

1. Extracting from *.tar archive

$ tar -xvpf backup.tar -C /home

-x: Extract from archive
-C: Used when you want to specify destination.
/home: extract backup.tar to /home folder.

2. Extracting from *.tar.gz archive

$ tar -xzvpf backup.tar.gz -C /home

-z: uncompressing a gzip archive

3. Extracting from *.tar.bz2 archive

$ tar -xjvpf backup.tar.bz2 -C /home

-j: uncompressing a bzip2 archive

How to list the content of different tar archives


1. List the content of *.tar archive

$ tar -tvf backup.tar

-t: used for listing

2. List the content of *.tar.gz archive

$ tar -tvfz backup.tar.gz

3. List the content of *.tar.bz2 archive

$ tar -tvfj backup.tar.bz2

That's it.
Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

0 comments:

Post a Comment