Linux TAR Commands with Examples for CentOS 7 / RHEL 7

What is TAR?

Tar (Tape archive) allows you to very easily, safely backup in Linux. If you have a small data to backing up it’s not a big deal. But if you want to backup large amount of data, yup it’s also easy but it’s take some time but not a big deal.

Backup Files Using TAR Archive

The first thing you need to do is type a command tar and we can take a backup. But there are some options we require to backup using tar, so we’ll discuss it firstly.

*** tar -option destination file source file

[[email protected]]#  tar  -cvpf   /etc.tar   /etc
[[email protected] ~]# ls   /
 bin     dev      file.txt  lost+found  opt   sbin     sys  var
 boot    etc      home      media       proc  selinux  tmp
 cgroup  etc.tar  lib       mnt         root  srv      usr

c -> create or override a backup file.

v -> Verbose (tell you what is going on during this process).

p -> preserving permission ( it is useful, if you are creating backups of web server and don’t put lowercase p, tar will not take it and we will face some problem while recovering data regarding permission. So after that recovery you need to go back and set permission and all that stuff once again. So keep this in mind that putp while taking backups.)

f -> Lowercase f is allow you to give tar a file name to create backup.

/etc.tar -> File name were you want to backup you data.

/etc -> The original data.

[[email protected]]# du   -sh  /etc
To check the size of a file.
[[email protected]]# file  /etc

To check the file system of a file.

And now to compress file nearly about 70% to compress already compress file, there we use some tools, these are following,

  1. gzip
  2. bzip2
  3. gzip
[[email protected]]# gzip   /etc.tar
[[email protected] ~]# ls /
 bin     dev         file.txt  lost+found  opt   sbin     sys  var
 boot    etc         home      media       proc  selinux  tmp
 cgroup  etc.tar.gz  lib       mnt         root  srv      usr
To unzip gzip compression
[[email protected] ~]# gunzip  /etc.tar.gz
[[email protected] ~]# bzip2  /etc.tar
[[email protected] ~]# ls  /
[[email protected] ~]# du  -sh  /etc.tar.bz2

Powerful than gzip.

To unzip bzip2 compression
[[email protected] ~]# bunzip2   /etc.tar.bz2
To extract TAR
[[email protected] ~]# tar  -xvf  /etc.tar
[[email protected] ~]# tar  -xvf  /etc.tar  -C  /root

x-> To extract.

C-> Destination folder

[[email protected] ~]# ls root
To check file without extraction.
[[email protected] ~]# tar  -tvf   /etc.tar

t-> List file in /etc.tar

Bzip and Gzip using TAR
[[email protected] ~]# tar  -cvpzf   /sbin.tar.gz
[[email protected] ~]# tar   -cvpjf   /bin.tar.bz2  /bin
To extract
[[email protected] ~]# tar   -xvf  /

Congratulations your Backup files are saved on TAR Archives.

Hot Right Now

Related Post