Skip to content

Linux tar command (file archiving)

The tar command in Linux is used to create file archives on Linux. These archives come from the time when backup tapes were used to create backup copies of data.

With the end of the use of backup tapes, the use of creating archives using the same software continued in the Unix and Linux world, through the tar command, which creates the files known as “tarball”.

Tarball files are bundles of files and directories that maintain the original directory and file structure in a tar archive, with the possibility of compressing data.

The command to package files and directories is tar. The name of this command comes from “Tape-Archive”. It reads files and directories and saves them to tape or file.

Along with the data, it saves important information such as the last modification, access permissions, and others. This makes it able to restore the original state of the data.

The tar command options are not so optional. It takes at least two arguments:

  • options: Tells what tar should do
  • [source]: If tar is used for backup, this parameter can be a file, a device, a directory to be copied;
  • [destination]: If the command is used for backup, this option will specify the destination for the data. It can be a tarball file or a device. If used to restore the files, it will specify a tarball file and a device from which the data will be extracted.

First, you must choose what tar should do using the options:

  • -c: Creates a new .tar file;
  • -u: Add more files to the .tar file only if they are new or modified;
  • -r: Adds the files specified at the end of the file .tar;
  • -g: Creates an incremental backup;
  • -t: Lists the contents of a .tar file;
  • -x: Extracts the .tar archive files;

It even has auxiliary options:

  • -j: Uses bzip2 to compress and unzip the .tar.bz2 files;
  • -J: Uses xz to compress and unzip the files .tar.xz
  • -z: Uses gzip to compress and unzip the files .tar.gz;
  • -v: Lists all processed files;
  • -f: Indicates that the destination is a file on disk, and not a magnetic tape drive;

The tar options can be combined into a single parameter such as “cvzf”.

Because it is a command that was originally designed to read/write to tape, to create a tar archive, or to read a tar archive from disk, the “f” option should always be used.

Examples:

To save a particular /var/lib/mysql directory to one in the /var/backup/mysql.tar.gz file:

$ tar cvzf /var/backup/mysql.tar.gz /var/mysql

To extract the same package:

$ tar xvzf /var/backup/mysql.tar.gz —C/

You can open the contents of a tarball file with the extension .tar.gz:

$ tar xvzf arquivo.tar.gz

If the file is compressed with bzip2, it must be unzipped by bunzip2:

$ tar xvjf arquivo.tar

In the case of files compressed with xz, you can use:

$ tar xVjf linux.tar.xz

In the graphical environment, you can unzip and extract a tarball file without much effort, just by clicking on the file. In this way, Linux will invoke the appropriate data compactor in the background, along with the tar to extract the data package in the current directory.

See the comparison between the compression performed by the gzip, bzip2 and xz compressors and an uncompressed tar archive:

$ ls -1sHS linux*
895M linux.tar
165M linux.tar.gz
126M linux.tar.bz2
105M linux-5.4.3.tar.xz

On a daily basis, it is recommended to memorize the following table to unzip files:

Extension**Used Compactor****Tar Option**
.tar.gzGzip$ tar xvzf arquivo.tar.gz
.tar.bz2Bzip2$ tar xvjf arquivo.tar. bz2
.tar.xzXz$ tar xVJF.tar.xz

Backup Using File Date with Tar

To create a package from a list of directories in a file with a date greater than 01/05/2020:

$ cat listabackup.txt /etc /var/lib/mysql /usr/local/apache2/conf
$ tar czPF backup.tar.gz -N 01/05/2020 -T listabackup.txt

Backup using SCSI

To save the /etc directory to SCSI tape on the /dev/st0 device:

$ tar cvz /dev/st0 /etc

To list the contents of a SCSI tape on the /dev/st0 device:

$ tar tfz /dev/st0

Extract Specific File

To extract the password file only:

$ tar xvfz /dev/st0 etc/passwd

Incremental Backup with Tar

The tar allows you to easily perform incremental backups using the “-g “option:

$ tar cvfz backup-full.tar.gz —g arquivo_controle.txt /dir1 /dir2

In this example, tar will create a first full backup called backup-full.tar.gz from the dir1 and dir2 directories. A checklist will be generated by tar with the name of arquivo_controle.txt.

To make incremental backups, which will contain only the files changed since the last backup (whether full or incremental), the same command is used, just changing the name of the tar file:

$ tar cvfz backup-incremental.tar.gz —g arquivo_controle.txt /dir1 /dir2

In this way, the tar will only place the files changed since the last backup in the backup-incremental.tar.gz.

To restore the backup made in this way, you must open the backup-full.tar.gz file:

$ tar xvzf backup-full.tar.gz

And all the incremental backups made since the full backup, in the order they were created:

$ tar xvzf backup-incremental.tar.gz