The tar command explained

The Linux tar command is the swiss army knife of the Linux admin when it comes to archiving or distributing files. Gnu Tar archives can contain multiple files and directories, file permissions can be preserved and it supports multiple compression formats. The name tar stands for "Tape Archiver", the format is an official POSIX standard.

Tar file formats

A short introduction into tar compression levels.

The common formats are tar.gz and tar.bz2. If your goal is fast compression, then use gzip. When the archive file size is critical, then use tar.bz2.

What is the tar command used for?

Here a few common use cases of the tar command.

Installing tar

The command is installed on most Linux systems by default. Here are the instructions to install tar in case that the command is missing.


Execute the following command as root user on the shell to install tar on CentOS.

yum install tar


This command will install tar on Ubuntu. The "sudo" command ensures that the apt command is run with root privileges.

sudo apt-get install tar


The following apt command installs tar on Debian.

apt-get install tar


The tar command is available for Windows as well, you can download it from the Gunwin project.

Create tar.gz Files

Here is the tar command that has to be run on the shell. I will explain the command line options below.

tar pczf myarchive.tar.gz /home/till/mydocuments

This command creates the archive myarchive.tar.gz which contains the files and folders from the path /home/till/mydocuments. The command line options explained:

Tar command examples

Example 1: Backup the /etc Directory Create a backup of the /etc config directory. The backup is stored in the root folder.

tar pczvf /root/etc.tar.gz /etc

The command should be run as root to ensure that all files in /etc are included in the backup. This time, I've added the [v] option in the command. This option stands for verbose, it tells tar to show all file names that get added into the archive. Example 2: Backup your /home directory Create a backup of your home directory. The backup will be stored in a directory /backup.

tar czf /backup/myuser.tar.gz /home/myuser

Replace myuser with your username. In this command, I've omitted the [p] switch, so the permissions get not preserved. Example 3: A file-based backup of MySQL databases The MySQL databases are stored in /var/lib/mysql on most Linux distributions. You can check that with the command:

ls /var/lib/mysql

Stop the database server to get a consistent MySQL file backup with tar. The backup will be written to the /backup folder. 1) Create the backup folder

mkdir /backup
chmod 600 /backup

2) Stop MySQL, run the backup with tar and start the database server again.

service mysql stop
tar pczf /backup/mysql.tar.gz /var/lib/mysql
service mysql start
ls -lah /backup

Extract tar.gz Files

The command to extract tar.gz files is:

tar xzf myarchive.tar.gz

The tar command options explained

The above tar command will silently extract that tar.gz file, it will show only error messages. If you like to see which files get extracted, then add the "v" option.

tar xzvf myarchive.tar.gz

The [v] option stands for verbose, it will show the file names while they get unpacked.

The tar command explained