fbpx tar Command in Linux: Compress and Extract Files | Certificação Linux

How to Use tar in Linux: Create, Extract, List .tar & .tar.gz Files

📅 01/01/2019 ⏱ 5 min ✍️ Uira Ribeiro
Linuxcommands
How to Use tar in Linux: Create, Extract, List .tar & .tar.gz Files
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

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

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

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

The command to package files and directories is the 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 last modification time, access permissions and others. This makes it capable of restoring the original state of the data.

The options of the tar command are not so optional after all. It receives at least two arguments:

  • options: Tells what tar should do
  • [source]: If tar is used to make a 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 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 through the options:

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

It also has auxiliary options:

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

The tar options can be combined in a single parameter like "cvzf".

Because it is a command that was originally made to read/write to tape, to create a tar archive or read a tar archive on disk, you must always use the "f" option.

Examples:

To save a specific directory /var/lib/mysql to a file /var/backup/mysql.tar.gz:

$ 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 decompressed 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 decompress and extract a tarball file without much effort, just by clicking on the file. This way Linux will invoke the appropriate data compressor in the background, along with tar to extract the data package in the current directory.

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

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

In daily use, it is recommended to memorize the following table to decompress files:

ExtensionCompressor UsedTar Option
.tar.gzGzip$ tar xvzf arquivo.tar.gz
.tar.bz2Bzip2$ tar xvjf arquivo.tar.bz2
.tar.xzXz$ tar xvJf arquivo.tar.xz

To create a package from a list of directories in a file and with the date after 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

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

$ tar cvz /dev/st0 /etc

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

$ tar tfz /dev/st0

To extract only the password file:

$ tar xvfz /dev/st0 etc/passwd

Tar allows you to make incremental backups easily, through 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 of the directories dir1 and dir2. A control list 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), use the same command, just changing the name of the tar file:

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

This way, tar will only put in backup-incremental.tar.gz the files changed since the last backup.

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

$ tar xvzf backup-full.tar.gz 

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

$ tar xvzf backup-incremental.tar.gz

Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp