Tar is archiving utility in Linux. With the tar command, we can archive multiple files and directories into a single file, an extension of that file is “.tar”. gzip and bzip compression techniques can be used with the tar command.

So in this article, we will cover all possible examples of tar.

Synatx

# tar [options] [name of tar file to be created] [list of files & directories to be included]

Options:

  • -d, –diff, –compare —> find differences between archive and file system
  • -r, –append —> append files to the end of an archive
  • -A, –catenate, –concatenate —-> append tar files to 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
  • –delete —> delete from the archive (not on mag tapes!)
  • -x, –extract, –get —-> extract files from an archive
  • -c, –create —-> create a new archive

Example 1: create an archive file.
Syntax:

# tar cvf [archive-Name].tar /[file-OR-Directories To Be archived]

Example:

# tar cvf /mnt/backup.tar /etc /usr

The above command will archive all the files and directories of /etc and /usr in the backup.tar file.

Example 2: Extract the data from the archive file.

# tar xpvf backup.tar -C /mnt/

Above Command will extract files or directories in /mnt folder.

Example 3: list archive file contents without extracting.

# tar tvf /mnt/backup.tar

Example 4: Create archive using gzip compression technique.

# tar zcpvf /mnt/backup-$(date +%d-%m-%Y).tar.gz /etc /usr

OR

# tar zcpvf /mnt/backup-$(date +%d-%m-%Y).tgz /etc /usr

Above Command will take the backup or archive of /etc & /usr folder according to the current date & also compress the tar file using gzip.

Example 5: Create archive using bzip compressio technique.

# tar jcpvf /mnt/backup-$(date +%d-%m-%Y).tar.bz2 /etc /usr

OR

# tar jcpvf /mnt/backup-$(date +%d-%m-%Y).tbz /etc /usr

Above Command will take the backup or archive of /etc & /usr folder according to the current date & also compress the tar file using bzip.

Example 6: Extract data from .tar.gz or .tgz and .tar.bz2 or .tbz.

# tar zxpvf /mnt/backup-21-03-2013.tgz -C /location-where-to-extract
# tar jxpvf /mnt/backup-21-03-2013.tbz -C /location-where-to-extract

Example 7: Extract a particular file and directory from the archive (.tar.gz OR .tgz AND .tar.bz2 OR .tbz).

# tar jxpvf backup-21-03-2013.tbz etc/passwd

Above Command will extract the passwd file from the backup tar file in the current directory.

# tar jxpvf backup-21-03-2013.tbz etc/apache2

The above command will extract the directory apache2 in the current directory

Example 8: Append new file or directory to the existing archive file using -r option.
We cannot add file or directory to a compressed archive , so first we have to uncompress it , so that it has an extension of “.tar”

# tar -rvf /mnt/backup-21-03-2013.tar vmlinuz-3.2.0-38-generic

The above command will append the vmlinuz file to backup tar file, to append directory to an archive replace the file with directory name in the above command.

Example 9: Excluding files and directories while taking backup using tar.
There are some scenarios where we have to take the backup of a directory and have to exclude particular file in that directory.

Example: Take the backup of /etc directory & exclude a subdirectory directory (/etc/sysconfig/network-scripts).

# tar -zcpvf etc-$(date +%d-%m-%Y).tgz --exclude /etc/sysconfig/network-scripts/ /etc/

Excluding files and directories using a file , this can be done via (–exclude-from or short form -X ).Create a file exclude.txt under /root folder, should have content: “/etc/sysconfig/network-scripts”

# tar -zcpvf etc-$(date +%d-%m-%Y).tgz -X /root/exclude.txt /etc/

The above command will take the backup of /etc folder excluding subdirectory“/etc/sysconfig/network-scripts”

Example 10: Backup using -T and -X option in tar.
Where:

  • -T: get names to extract or create from FILE
  • -X: exclude patterns listed in FILE

In Some scenario, where we want to specify the files to be backed up and some files to be excluded from the backup.

Create file /root/include.txt:

# cat /root/include.txt
/etc
/home

Create a file /root/exclude.txt:

# cat /root/exclude.txt
/etc/sysconfig/network-scripts
/etc/fstab

Now Take the backup using below command:

# tar -zcpvf backup-$(date +%d-%m-%Y).tgz -T /root/include.txt -X /root/exclude.txt

Above command will take the backup of all files and directories mentioned in include.txt and will exclude files & directories mentioned in the exclude.txt file.