What is tar? The term tar means tape archive. In good old day’s system admin used to take backups on tape drives. But now we use tar utility to take backup to other digital media. Majority of Linux system administrators use tar utility in linux to compress archive file which is usually called tarball , simply tar , bzip and gzip. This utility is tremendously used to create compressed archive files which We have already discussed file compression and archive in our tutorial. So you can also refer to this tutorials
COMPRESSION AND ARCHIVING PART1 and  COMPRESS AND ARCHIVE PART 2




In this article we will going to see practical usage of tar command examples including how to create archive files using (tar, tar.gz and tar.bz2) compression, how to extract archive file, how to extract a single file, how to view content of file, how to verify a file, how to add files or directories to archive file, and how to estimate the size of tar archive file, etc.

1. To create an tar archive of a file using tar command

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

Syntax : tar cvf archivefile.tar  /path/to/file

c – this switch creates a new archive
v – this switch is for verbose meaning it displays list files which are processed.
f – this switch is for file name

[root@shahid ~]# ls -ltrh
total 16K
-rw-------. 1 root root 1.6K Feb  3 17:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.7K Feb  3 17:44 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root   61 Mar  2 11:19 epoch_converter.pl
-rw-r--r--. 1 root root   24 Apr 10 21:50 health.txt

In above output we can see that in current directory we have file health.txt. we will archive this file now

[root@shahid ~]# tar -cvf health.tar health.txt
health.txt
[root@shahid ~]# ll
total 28
-rw-------. 1 root root  1597 Feb  3 17:42 anaconda-ks.cfg
drwxr-xr-x. 2 root root    42 Jun 21 09:35 DirA
-rwxr-xr-x. 1 root root    61 Mar  2 11:19 epoch_converter.pl
-rw-r--r--. 1 root root 10240 Jun 21 09:34 health.tar
-rw-r--r--. 1 root root    24 Apr 10 21:50 health.txt
-rw-r--r--. 1 root root  1645 Feb  3 17:44 initial-setup-ks.cfg

In above example we have archived file health .txt  and now you can see health.tar archived file in current directory.




2. Creating compressed tar archive of file

To create zipped archive or compressed archive of file we will use option z which is gzip compression

Syntax : tar cvzf archivename.tar.gz filename.txt

[root@shahid ~]# tar cvzf health.tar.gz health.txt
health.txt
[root@shahid ~]# ls -ltrh
total 32K
-rw-------. 1 root root 1.6K Feb  3 17:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.7K Feb  3 17:44 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root   61 Mar  2 11:19 epoch_converter.pl
-rw-r--r--. 1 root root   24 Apr 10 21:50 health.txt
-rw-r--r--. 1 root root  10K Jun 21 09:34 health.tar
drwxr-xr-x. 2 root root   42 Jun 21 09:35 DirA
-rw-r--r--. 1 root root  144 Jul 31 12:09 health.tar.gz
[root@shahid ~]#

In above example you can see file zise difference of health.tar (uncompressed archive) and health.tar.gz (compressed archive)

3. Create tar.bz2 Archive File

Now we will create tar archive file with bzip compression which takes more time to compress and decompress but is highly compressed archive in other words it takes less space that gzip compression
we wil use option j instead of z
syntax : tar cvfj archive.tar.bz filename

[root@shahid ~]# tar cvfj health.tar.bz health.txt
health.txt
[root@shahid ~]# ls -ltrh
total 36K
-rw-------. 1 root root 1.6K Feb  3 17:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.7K Feb  3 17:44 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root   61 Mar  2 11:19 epoch_converter.pl
-rw-r--r--. 1 root root   24 Apr 10 21:50 health.txt
-rw-r--r--. 1 root root  10K Jun 21 09:34 health.tar
drwxr-xr-x. 2 root root   42 Jun 21 09:35 DirA
-rw-r--r--. 1 root root  144 Jul 31 12:09 health.tar.gz
-rw-r--r--. 1 root root  141 Aug  1 09:01 health.tar.bz

in above example you can see diff between gz and bz compressed archives. 4.

4. Create tar archive of diretory

creating archive of directory is same as file. Syntax: tar cvfj archivedirectory.tar.bz /path/to/directory

[root@shahid ~]# ls -ltrh
total 40K
-rw-------. 1 root root 1.6K Feb  3 17:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.7K Feb  3 17:44 initial-setup-ks.cfg
-rwxr-xr-x. 1 root root   61 Mar  2 11:19 epoch_converter.pl
-rw-r--r--. 1 root root   24 Apr 10 21:50 health.txt
-rw-r--r--. 1 root root  10K Jun 21 09:34 health.tar
-rw-r--r--. 1 root root  144 Jul 31 12:09 health.tar.gz
-rw-r--r--. 1 root root  141 Aug  1 09:01 health.tar.bz
drwxr-xr-x. 3 root root   53 Aug  1 09:14 DirA
-rw-r--r--. 1 root root  177 Aug  1 09:16 DirB.tar.bz
[root@shahid ~]# tar tvf Dir
DirA/        DirB.tar.bz

5. To list content of archive using tar command <v/h4>

You can View the file content of tar archive without extracting using option tvf
You can view the *.tar file content before extracting as shown below.

[root@shahid ~]# tar tvf health.tar
-rw-r--r-- root/root        24 2016-04-10 21:50 health.txt
[root@shahid ~]#

To view content of tar.gz archive you can use option tvfz

[root@shahid ~]# tar tvfz health.tar.gz
-rw-r--r-- root/root        24 2016-04-10 21:50 health.txt
[root@shahid ~]#

to view contents of tar.bz file you can use option tvfj

[root@shahid ~]# tar tvfj health.tar.bz
-rw-r--r-- root/root        24 2016-04-10 21:50 health.txt

6. To untar archived tar file

You can extract a tar file by using following command with an option x (which is for extract). Below example shows how command is used to untar file
health.tar. This will untar in current working directory

[root@shahid ~]# ll
total 36
-rw-------. 1 root root  1597 Feb  3  2016 anaconda-ks.cfg
drwxr-xr-x. 3 root root    53 Aug  1 09:14 DirA
-rw-r--r--. 1 root root   177 Aug  1 09:16 DirB.tar.bz
-rwxr-xr-x. 1 root root    61 Mar  2 11:19 epoch_converter.pl
-rw-r--r--. 1 root root 10240 Jun 21 09:34 health.tar
-rw-r--r--. 1 root root   141 Aug  1 09:01 health.tar.bz
-rw-r--r--. 1 root root   144 Jul 31 12:09 health.tar.gz
-rw-r--r--. 1 root root  1645 Feb  3 17:44 initial-setup-ks.cfg
[root@shahid ~]# tar xvf health.tar
health.txt
[root@shahid ~]# ll
total 40
-rw-------. 1 root root  1597 Feb  3  2016 anaconda-ks.cfg
drwxr-xr-x. 3 root root    53 Aug  1 09:14 DirA
-rw-r--r--. 1 root root   177 Aug  1 09:16 DirB.tar.bz
-rwxr-xr-x. 1 root root    61 Mar  2 11:19 epoch_converter.pl
-rw-r--r--. 1 root root 10240 Jun 21 09:34 health.tar
-rw-r--r--. 1 root root   141 Aug  1 09:01 health.tar.bz
-rw-r--r--. 1 root root   144 Jul 31 12:09 health.tar.gz
-rw-r--r--. 1 root root    24 Apr 10 21:50 health.txt
-rw-r--r--. 1 root root  1645 Feb  3  2016 initial-setup-ks.cfg
[root@shahid ~]#

if you want to extract in different directory then you can use option -C followed by path to directory with command tar xvf

[root@shahid ~]# ll
total 40
-rw-------. 1 root root  1597 Feb  3  2016 anaconda-ks.cfg
drwxr-xr-x. 3 root root    53 Aug  1 09:14 DirA
-rw-r--r--. 1 root root   177 Aug  1 09:16 DirB.tar.bz
-rwxr-xr-x. 1 root root    61 Mar  2 11:19 epoch_converter.pl
-rw-r--r--. 1 root root 10240 Jun 21 09:34 health.tar
-rw-r--r--. 1 root root   141 Aug  1 09:01 health.tar.bz
-rw-r--r--. 1 root root   144 Jul 31 12:09 health.tar.gz
-rw-r--r--. 1 root root    24 Apr 10 21:50 health.txt
-rw-r--r--. 1 root root  1645 Feb  3  2016 initial-setup-ks.cfg
[root@shahid ~]# ll DirA
total 0
drwxr-xr-x. 2 root root 42 Aug  1 09:15 DirB
-rw-r--r--. 1 root root  0 Aug  1 09:14 file1
-rw-r--r--. 1 root root  0 Aug  1 09:14 file2
-rw-r--r--. 1 root root  0 Aug  1 09:14 file3
[root@shahid ~]# tar xvf health.tar -C DirA/
health.txt
[root@shahid ~]# ll DirA
total 4
drwxr-xr-x. 2 root root 42 Aug  1 09:15 DirB
-rw-r--r--. 1 root root  0 Aug  1 09:14 file1
-rw-r--r--. 1 root root  0 Aug  1 09:14 file2
-rw-r--r--. 1 root root  0 Aug  1 09:14 file3
-rw-r--r--. 1 root root 24 Apr 10 21:50 health.txt
[root@shahid ~]#

7. To compress archive and delete original files using tar command

To make compressed tar of contents of files and remove original contents in current directory you can use below command. This command helps in situation where you don't have enough space. This process archive contents while already archived contents are being deleted simultaneously tar cvfj archive.tar.bz * --remove-files
[root@shahid DirB]# ls -ltrh
total 0
-rw-r--r--. 1 root root 0 Aug  8 12:07 fileA
-rw-r--r--. 1 root root 0 Aug  8 12:07 fileB
-rw-r--r--. 1 root root 0 Aug  8 12:07 fileC
[root@shahid DirB]# tar cvfj files.tar.bz * --remove-files
fileA
fileB
fileC
[root@shahid DirB]# ls -ltrh
total 4.0K
-rw-r--r--. 1 root root 143 Aug  8 12:07 files.tar.bz
[root@shahid DirB]#

More commands coming soon .....

SUBSCRIBE OUR NEWSLETTER
I agree to have my personal information transfered to MailChimp ( more information )
Join us by subscribing to our newsletter and learn IT subjects for free
We hate spam. Your email address will not be sold or shared with anyone else.