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:
First, you must choose what tar should do using the options:
It even has auxiliary options:
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.gz | Gzip | $ tar xvzf arquivo.tar.gz |
.tar.bz2 | Bzip2 | $ tar xvjf arquivo.tar. bz2 |
.tar.xz | Xz | $ tar xVJF.tar.xz |
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
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
To extract the password file only:
$ tar xvfz /dev/st0 etc/passwd
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
The sudo command on Linux executes a given command as if it were another user.…
The ss command on Linux is extremely useful for investigating sockets, providing various information about…
Free Linux command shows the amount of total memory in use and available, as well…
The shell has structures for testing conditions and executing certain program sequences several times (loop),…
The /etc/fstab file stores the configuration of which devices should be mounted and what is…
The Netcat Command in Linux or nc is a utility used to do “almost anything”…
This website uses cookies.