Pages

Saturday, February 16, 2013

TAR Command


The tar (i.e., tape archive) command is used to convert a group of files into an archive.

An archive is a single file that contains any number of individual files plus information to allow them to be restored to their original form by one or moreextraction programs. Archives are convenient for storing files as well as for for transmitting data and distributing programs. Moreover, they are very easy to work with, often much more so than dealing with large numbers of individual files.

Although tar was originally designed for backups on magnetic tape, it can now be used to create archive files anywhere on a filesystem. Archives that have been created with tar are commonly referred to as tarballs.

 1. Creating an archive using tar command

Creating an uncompressed tar archive using option cvf
This is the basic command to create a tar archive.

$ tar cvf archive_name.tar dirname/
In the above command:

c – create a new archive
v – verbosely list files which are processed.
f – following is the archive file name
Creating a tar gzipped archive using option cvzf

2.The above tar cvf option, does not provide any compression. To use a gzip compression on the tar archive, use the z option as shown below.

tar cvzf archive_name.tar.gz dirname/

z – filter the archive through gzip
Note: .tgz is same as .tar.gz

3.Files can be added to an existing archive using the -r option
tar -rf file.tar file7

4. The --delete option allows specified files to be completely removed from a tar file
tar -f file.tar --delete file1 file2

5. The -t option tells tar to list the contents of an uncompressed archive without performing an extraction. Thus, the following would list the contents of file.tar
tar -tf file.ta

6.In order to unpack a tar file, the -x (for extract) and -f options are required
tar -xvf file.tar

For more info visit: