The Unix / Linux operating system has been equipped with tools to protect and duplicate data, so that if there is damage to the system then the data can be returned (restore). Nowadays, data is a very important asset, so if the data was loss or damage, it will cause huge losses. This opportunity has given birth to several application systems that specialize in data protection systems, such like : Netbackup Veritas, EMC Networker, Veeam, Amanda and several backup applications.
There are several command lines that Unix/Linux OS had for backup purpose, namely: tar, cpio, dump
. This article will discuss how to use tar
command line on Linux/Unix environment. Where another command lines will be discussed on other article.
tar Command
The tar
command saves many files together into a single tape or disk archive, and can restore individual files from the archive. Its primary function command is to create backups.
tar Operation Mode
Main operation mode:
-A, --catenate, --concatenate
. Append tar files to an archive
-c, --create
. Create a new archive
-d, --diff, --compare
. Find differences between archive and file system
--delete
. Delete from the archive (not on mag tapes!)
-r, --append
. Append files to the end of an archive
-t, --list
. List the contents of an archive
--test-label
. Test the archive volume label and exit
–u, --update
. Only append files newer than copy in archive
-x, --extract, --get
. Extract files from an archive
Common options:
-C, --directory=DIR
. Change to directory DIR
-f, --file=ARCHIVE
. Use archive file or device ARCHIVE
-j, --bzip2
. Filter the archive through bzip2
-J, --xz
. Filter the archive through xz
-p, --preserve-permissions
. Extract information about file permissions (default for superuser)
-v, --verbose
. Verbosely list files processed
-z, --gzip
. Filter the archive through gzip
tar Command For Backup Purpose
Example for tar in use for backup. In this case we will backup the mountpoint /etc/yum.repos.d
and /etc/cups
and named it as test01.tar
. The command is as follow :
[ramans@localhost yum.repos.d]$ sudo tar cvf test01.tar /etc/yum.repos.d /etc/cups tar: Removing leading `/' from member names /etc/yum.repos.d/ /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-CR.repo /etc/yum.repos.d/CentOS-Debuginfo.repo /etc/yum.repos.d/CentOS-Media.repo /etc/yum.repos.d/CentOS-Sources.repo /etc/yum.repos.d/CentOS-Vault.repo /etc/yum.repos.d/CentOS-fasttrack.repo /etc/yum.repos.d/docker-ce.repo tar: /etc/yum.repos.d/test01.tar: file is the archive; not dumped /etc/cups/ /etc/cups/cups-browsed.conf /etc/cups/classes.conf /etc/cups/client.conf /etc/cups/cups-files.conf /etc/cups/cupsd.conf /etc/cups/cupsd.conf.default /etc/cups/interfaces/ /etc/cups/lpoptions /etc/cups/ppd/ /etc/cups/printers.conf /etc/cups/snmp.conf /etc/cups/ssl/ /etc/cups/paps.convs /etc/cups/cups-browsed.conf.rpmnew /etc/cups/subscriptions.conf.O /etc/cups/subscriptions.conf

If we verify, the result is as below :
[ramans@localhost yum.repos.d]$ ls -ltr *.tar -rw-r--r--. 1 root root 71680 Dec 18 18:19 test01.tar

We also could verify the content of test01.tar file, with teh command sudo tar tvf test01.tar
, as shown below :
[ramans@localhost yum.repos.d]$ sudo tar tvf test01.tar drwxr-xr-x root/root 0 2019-12-18 18:19 etc/yum.repos.d/ -rw-r--r-- root/root 1664 2019-09-05 20:05 etc/yum.repos.d/CentOS-Base.repo -rw-r--r-- root/root 1309 2019-09-05 20:05 etc/yum.repos.d/CentOS-CR.repo -rw-r--r-- root/root 649 2019-09-05 20:05 etc/yum.repos.d/CentOS-Debuginfo.repo -rw-r--r-- root/root 630 2019-09-05 20:05 etc/yum.repos.d/CentOS-Media.repo -rw-r--r-- root/root 1331 2019-09-05 20:05 etc/yum.repos.d/CentOS-Sources.repo -rw-r--r-- root/root 6639 2019-09-05 20:05 etc/yum.repos.d/CentOS-Vault.repo -rw-r--r-- root/root 314 2019-09-05 20:05 etc/yum.repos.d/CentOS-fasttrack.repo -rw-r--r-- root/root 2424 2019-10-19 04:57 etc/yum.repos.d/docker-ce.repo drwxr-xr-x root/lp 0 2019-12-18 17:25 etc/cups/ -rw-r--r-- root/root 1029 2019-12-10 23:10 etc/cups/cups-browsed.conf -rw------- root/lp 0 2019-08-09 06:07 etc/cups/classes.conf -rw-r--r-- root/lp 0 2019-08-09 06:07 etc/cups/client.conf -rw-r----- root/lp 3091 2019-12-12 19:15 etc/cups/cups-files.conf -rw-r----- root/lp 4504 2019-08-09 06:07 etc/cups/cupsd.conf -rw-r----- root/lp 4504 2019-08-09 06:07 etc/cups/cupsd.conf.default drwxr-xr-x root/root 0 2019-08-09 06:07 etc/cups/interfaces/ -rw-r--r-- root/lp 0 2019-08-09 06:07 etc/cups/lpoptions drwxr-xr-x root/lp 0 2019-08-09 06:07 etc/cups/ppd/ -rw------- root/lp 0 2019-08-09 06:07 etc/cups/printers.conf -rw-r--r-- root/lp 186 2019-08-09 06:07 etc/cups/snmp.conf drwx------ root/lp 0 2019-08-09 06:07 etc/cups/ssl/ -rw-r--r-- root/root 319 2015-06-22 19:22 etc/cups/paps.convs -rw-r--r-- root/root 16072 2019-08-09 06:09 etc/cups/cups-browsed.conf.rpmnew -rw-r----- root/lp 111 2019-12-18 17:24 etc/cups/subscriptions.conf.O -rw-r----- root/lp 412 2019-12-18 17:25 etc/cups/subscriptions.conf

If we want to extract the file, we just add the x option
, and the extracted file will be saved on the other location ( at this example will be saved on /home/ramans
directory), the command could be like :
[ramans@localhost yum.repos.d]$ sudo tar -xvf test01.tar --directory /home/ramans/
An other examples of tar command purpose is as below :
[ramans@localhost /]$ tar -cf archive.tar foo bar # Create archive.tar from files foo and bar. [ramans@localhost /]$ tar -tvf archive.tar # List all files in archive.tar verbosely. [ramans@localhost /]$ tar -xf archive.tar # Extract all files from archive.tar.
One thought on “How To Use tar Command Line on Linux/Unix”