Understanding tar utility

 

In this article, we are going to look at tar utility in Unix systems. Mostly the tar is used for backup purposes.

tar – Tape Archive

Here is the example,

Now we are going to take a tar backup of /etc directory from the server.

[root@server ~]# tar cvf /home/myuser/etc.tar /etc

c – create
v – verbose
f – file name (tar file name)

[root@server ~]# pwd
/home/myuser/
[root@server myuser]# ll
total 27748
drwxr-xr-x. 2 myuser myuser 6 Feb 9 2015 Desktop
-rw-r--r--. 1 root root 28405760 Jul 30 03:09 etc.tar
drwxr--r-x. 2 root root 33 Apr 22 2016 log
-rw-rw-r--. 1 myuser myuser 820 Aug 11 2015 VNCHOWTO
-rw-------. 1 myuser myuser 67 Mar 18 2016 xrdp-chansrv.log
[root@server myuser]#

To verify the file type,

[root@server myuser]# file etc.tar 
etc.tar: POSIX tar archive (GNU)
[root@server myuser]#

Did you feel still your tar backup file size is big? If yes, please try to compress it in a more efficient way.

[root@server ~]# pwd
/home/myuser/
[root@server myuser]# tar czvf etc1.tgz /etc/
[root@server myuser]# ll
total 47060
drwxr-xr-x. 2 myuser myuser 6 Feb 9 2015 Desktop
-rw-r--r--. 1 root root 9885797 Jul 30 03:16 etc1.tgz
-rw-r--r--. 1 root root 28405760 Jul 30 03:09 etc.tar
drwxr--r-x. 2 root root 33 Apr 22 2016 log
-rw-rw-r--. 1 myuser myuser 820 Aug 11 2015 VNCHOWTO
-rw-------. 1 myuser myuser 67 Mar 18 2016 xrdp-chansrv.log
[root@server myuser]# 
[root@server myuser]# file etc1.tgz 
etc1.tgz: gzip compressed data, from Unix, last modified: Sun Jul 30 03:16:48 2017
[root@server myuser]#

Now see the size difference between etc.tar and etc1.tgz files. The etc1.tgz file size is smaller than etc.tar file.

You can view the tar file contents without untar it. To do the same,

[root@server myuser]# tar tvf etc.tar | more
drwxr-xr-x root/root 0 2017-07-30 03:05 etc/
drwxr-xr-x root/root 0 2017-07-07 08:01 etc/lvm/
drwx------ root/root 0 2017-06-09 06:00 etc/lvm/archive/
drwx------ root/root 0 2017-06-09 06:00 etc/lvm/backup/
drwx------ root/root 0 2017-06-09 06:00 etc/lvm/cache/

t – To list the content from an archive

You can untar the archive by using below command.

[root@server myuser]# tar xvf etc.tar -C /tmp
[root@server myuser]# cd /tmp/
[root@server tmp]# ll
total 12
drwxr-xr-x. 98 root root 8192 Jul 30 03:05 etc
drwx------. 2 myuser myuser 23 Jul 30 03:05 ssh-zazknH2cTbEE
drwx------. 3 root root 16 Jul 30 03:05 systemd-private-568cea610aa5466cbb2200b23d77293b-colord.service-jDlTbO
drwx------. 3 root root 16 Jul 30 03:05 systemd-private-568cea610aa5466cbb2200b23d77293b-rtkit-daemon.service-NeGvcJ
drwx------. 2 myuser myuser 6 Jun 7 22:05 tracker-extract-files.1001
[root@server tmp]#

x – To extract contents from an archive
C – To change the Directory

How to retrive the files from tar backup?

[root@server etc]# pwd
/tmp/etc
[root@server etc]# rm wgetrc 
rm: remove regular file ‘wgetrc’? y
[root@server etc]# tar xvf /home/myuser/etc.tar -C / etc/wgetrc
etc/wgetrc

 

 

Related posts

Leave a Comment