Using TAR Command Practically in Linux

tar is a commonly used command to create archive files. TAR stands for Tape ARchive. It is important to note that tar is only an archiving tool and doesn’t compress files. It only collects files and their metadata together and produces one single file. We can use other arguments to compress the archived files.

How to archive files using tar command?

Syntax:

# tar -cvf [filename].tar [list of files/directories]

Here, c - Create a new archive. v – Verbosely list files processed. f – Following is the archive file name.

For Example:

$ tar -cvf example.tar tempdir/
tempdir/
tempdir/yum_save_tx-2016-05-30-12-04ElkRUl.yumtx
tempdir/yum.log
tempdir/cron.txt
tempdir/lost+found/
tempdir/yum_save_tx-2016-06-13-10-04Tmc6vi.yumtx
tempdir/yum_save_tx-2016-05-29-23-26Ba1zrB.yumtx
tempdir/yum_save_tx-2016-06-15-17-42G0BG7Y.yumtx

$ ls -l
total 24
-rw-rw-r--. 1 geek geek 20480 Jun 21 21:47 example.tar
drwxrwxr-x. 3 geek geek 4096 Jun 21 21:14 tempdir

How to create a tar gzipped archive?

Syntax:

# tar -cvzf [filename].tar.gz [list of files/directories]

Here, z: Filter the archive through gzip

For example:

$ tar -cvzf example.tar.gz tempdir/
tempdir/
tempdir/yum_save_tx-2016-05-30-12-04ElkRUl.yumtx
tempdir/yum.log
tempdir/cron.txt
tempdir/lost+found/
tempdir/yum_save_tx-2016-06-13-10-04Tmc6vi.yumtx
tempdir/yum_save_tx-2016-05-29-23-26Ba1zrB.yumtx
tempdir/yum_save_tx-2016-06-15-17-42G0BG7Y.yumtx

[geek@centosprod ~]$ ls -l
total 28
-rw-rw-r--. 1 geek geek 1238 Jun 21 21:27 example.tar.gz
drwxrwxr-x. 3 geek geek 4096 Jun 21 21:14 tempdir

How to create a bzipped tar archive?

Syntax:

# tar -cvjf [filename].tar.bz2 [list of files/directories]

Here, j – filter the archive through bzip2.

For example:

$ tar -cvjf example.tar.bz2 tempdir/
tempdir/
tempdir/yum_save_tx-2016-05-30-12-04ElkRUl.yumtx
tempdir/yum.log
tempdir/cron.txt
tempdir/lost+found/
tempdir/yum_save_tx-2016-06-13-10-04Tmc6vi.yumtx
tempdir/yum_save_tx-2016-05-29-23-26Ba1zrB.yumtx
tempdir/yum_save_tx-2016-06-15-17-42G0BG7Y.yumtx

$ ls -l
total 32
-rw-rw-r--. 1 geek geek 1449 Jun 21 21:33 example.tar.bz2
drwxrwxr-x. 3 geek geek 4096 Jun 21 21:14 tempdir

How to untar an archived file?

A *.tar or *.tar.gz, *.tar.bz2 file can be untar using below command.

Syntax:

# tar -xvf [filename.tar/.tar.gz/.tar.bz2

For example:

$ tar -xvf example.tar
tempdir/
tempdir/yum_save_tx-2016-05-30-12-04ElkRUl.yumtx
tempdir/yum.log
tempdir/cron.txt
tempdir/lost+found/
tempdir/yum_save_tx-2016-06-13-10-04Tmc6vi.yumtx
tempdir/yum_save_tx-2016-05-29-23-26Ba1zrB.yumtx
tempdir/yum_save_tx-2016-06-15-17-42G0BG7Y.yumtx

How to list contents of an archive files *.tar, *.tar.gz, *.tar.bz2?

Syntax:

# tar -tvf [filename].tar / tar.gz / tar.bz2
$ tar -tvf example.tar
drwxrwxr-x geek/geek 0 2016-06-21 21:14 tempdir/
-rw------- geek/geek 867 2016-06-20 21:05 tempdir/yum_save_tx-2016-05-30-12-04ElkRUl.yumtx
-rw------- geek/geek 0 2016-06-20 21:05 tempdir/yum.log
-rw-rw-r-- geek/geek 0 2016-06-15 13:03 tempdir/cron.txt
drwx------ geek/geek 0 2016-05-29 21:13 tempdir/lost+found/
-rw------- geek/geek 4303 2016-06-20 21:05 tempdir/yum_save_tx-2016-06-13-10-04Tmc6vi.yumtx
-rw------- geek/geek 1976 2016-06-20 21:05 tempdir/yum_save_tx-2016-05-29-23-26Ba1zrB.yumtx
-rw------- geek/geek 519 2016-06-20 21:05 tempdir/yum_save_tx-2016-06-15-17-42G0BG
$ less example.tar

How to add files or directories to existing tar archived file?

Syntax:

# tar -rvf [filename].tar [file/directory]

for example:

$ tar -rvf example.tar xyz.txt
xyz.txt
$ tar -tvf example.tar
drwxrwxr-x geek/geek 0 2016-06-21 21:14 tempdir/
-rw------- geek/geek 867 2016-06-20 21:05 tempdir/yum_save_tx-2016-05-30-12-04ElkRUl.yumtx
-rw------- geek/geek 0 2016-06-20 21:05 tempdir/yum.log
-rw-rw-r-- geek/geek 0 2016-06-15 13:03 tempdir/cron.txt
drwx------ geek/geek 0 2016-05-29 21:13 tempdir/lost+found/
-rw------- geek/geek 4303 2016-06-20 21:05 tempdir/yum_save_tx-2016-06-13-10-04Tmc6vi.yumtx
-rw------- geek/geek 1976 2016-06-20 21:05 tempdir/yum_save_tx-2016-05-29-23-26Ba1zrB.yumtx
-rw------- geek/geek 519 2016-06-20 21:05 tempdir/yum_save_tx-2016-06-15-17-42G0BG7Y.yumtx
-rw-rw-r-- geek/geek 0 2016-06-21 22:04 xyz.txt

Source: man TAR(1)