One of the oldest and most common commands for creating and working with backup
archives is the
tar command.
With tar, users can gather large sets of files into a single file (archive). The archive can be
compressed using
gzip, bzip2, or xz compression.
Operate the tar command
To use the tar command , one of the three following actions is req uired:
- c (create an archive)
- t (list the contents of an archive)
- x (extract an archive)
Commonly used options are:
- f file name (file name of the archive to operate on)
- v (verbosity; useful to see which files get added to or extracted from the archive)
- p Preserve the permissions of files and directories when extracting an archive,
without subtracting the umask.
Step 1 : Create a copy of /etc directory
[root@rhel7 ~]# cp -r /etc /home/sirat/etc
[root@rhel7 ~]# du -sh /home/sirat/etc/
34M /home/sirat/etc/
Step 2: tar archive of etc directory
[root@rhel7 sirat]# tar -cf etc.tar etc
[root@rhel7 sirat]# du -sh *
34M etc
30M etc.tar
8.0K test
[root@rhel7 sirat]#
Note Here after executing size reduce 4 MB
List contents of a tar archive
[root@rhel7 sirat]# tar tf /root/etc . tar
etc/
etc/fstab
etc/crypttab
etc/mtab
etc/yum/yum-cron.conf
etc/dhcp/
.
.
.
Step 3: Extract archive of etc.tar to extract directory and check the size also
[root@rhel7 extract]# tar -xf /home/sirat/etc.tar
[root@rhel7 extract]# cd ../
[root@rhel7 sirat]# du -sh *
34M etc
30M etc.tar
34M extract
8.0K test
[root@rhel7 sirat]#
Step 4: Compressed tar archive file
[root@rhel7 sirat]# tar -czf etc.tar.gz etc
[ gzip compression ]
[root@rhel7 sirat]# tar -xzf etc.tar.gz etc
[ gzip un compression ]
[root@rhel7 sirat]# tar -cjf etc.tar.bz2 etc
[ bzip2 compression ]
[root@rhel7 sirat]# tar -xjf etc.tar.bz2 etc
[ bzip2 un compression ]
[root@rhel7 sirat]# tar -cJf etc.tar.xz etc
[ xz compression ]
[root@rhel7 sirat]# tar -xJf etc.tar.xz
[ xz un compression ]
[root@rhel7 sirat]# du -sh *
34M etc
30M etc.tar
[ tar compression ]
8.3M etc.tar.gz
[ gzip compression ]
7.0M etc.tar.bz2
[ bzip2 compression ]
5.7M etc.tar.xz
[ xz compression ]
8.0K test
[root@rhel7 sirat]#
To create a compressed tar archive, one of the following tar options can be specified:
. z for gzip compression (filename.tar.gz or filename.tgz)
· j for bzip2 compression (filename.ta r.bz2)
. J for xz compression (filename.tar.xz)
[root@rhel7 sirat]# gzip etc.tar
[ gzip compression ]
[root@rhel7 sirat]# gunzip etc.tar.gz
[ gzip un compression ]
[root@rhel7 sirat]# bzip2 etc.tar
[ bzip2 compression ]
[root@rhel7 sirat]# bunzip2 etc.tar.bz2
[ bzip2 un compression ]
[root@rhel7 sirat]# xz etc.tar [ xz compression ]
[root@rhel7 sirat]# unxz etc.tar.xz [ xz un compression ]