Linux File Compression: tar, gzip, bzip2, zip, and xz
File compressors in Linux are used daily by administrators.
Learn about the tar command and gzip, bzip2, and xz compressors to combine and compress files in Linux. Essential for any administrator.
tar command
Usage:
$ tar [ctxurgjzpvfNCMF] [destination] [source]
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, access permissions and others. This makes it capable of restoring the original state of the data.
The tar command takes two arguments. They are:
[source]: If tar is used for backup, this parameter can be a file, a device, or 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.
The most common options are:
- -c: Creates a new .tar file;
- -t: Lists the contents of a .tar file;
- -x: Extracts files from a .tar archive;
- -u: Adds files to the .tar archive only if they are new or modified;
- -r: Adds the specified files at the end of the .tar archive;
- -g: Creates an incremental backup;
- -j: Uses bzip2 to compress and decompress .tar files;
- -z: Uses gzip to compress and decompress .tar files;
- -p: Extracts files with the same creation permissions;
- -v: Lists all processed files;
- -f: Indicates that the destination is a file on disk, not a magnetic tape unit;
- -N: date Only saves files newer than the specified date;
- -C: Specifies the location where data will be extracted to;
- -M: Enables multiple volumes;
- -T: Creates a .tar package from a list of files and directories.
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 /
To create a backup of the /home/documentos directory to multiple floppies:
# tar cvMf /dev/fd0 /home/documentos
To create a package from a list of directories in a file and with the date greater than 05/01/2004.
# cat listabackup.txt
/etc
/var/lib/mysql
/usr/local/apache2/conf
# tar czPf backup-010504.tar.gz -N 01/05/2004 -T listabackup.txt
To save the /etc directory to a SCSI tape at 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
Data backup operations can be automated through shell scripts and scheduled in cron, requiring only the administrator to swap media and review the reports.
Data Compressors
gzip and gunzip command
For greater efficiency and backup media economy, there is the data compression resource.
The first widely used data compressor is gzip. It uses a compression algorithm called Lempel-Ziv. This technique finds duplicated characters in the input data. The second occurrence of characters is replaced by pointers to the previous reference, in the form of distance and length pairs. When compressing a file, gzip adds the .gz suffix.
To compress a file:
$ gzip file
To decompress a file:
$ gzip –d file.gz
Or
$ gunzip file.gz
bzip2 and bunzip2 command
The bzip2 compressor uses the Burrows-Wheeler and Huffman algorithm. This technique operates on large data blocks. The larger the block size, the higher the compression rate achieved. It is considered better than conventional data compressors. When compressing a file, bzip2 adds the .bz2 suffix.
To compress a file:
$ bzip2 file
To decompress a file:
$ bzip2 –d file.bz2
Or
$ bunzip2 file.bz2
There are some cases where the compressed file may become larger than the original file. This can happen if the algorithm used doesn't find occurrences to perform the data compression and the compressor header is added to the original file.
xz command
Additionally, we have the xz data compressor, which uses an algorithm similar to gzip. It produces files with the .xz or .lzma extension.
To compress a file:
$ xz file
To decompress:
$ xz --decompress file.xz
To give you an idea of the difference between the three compressors gzip, bzip2 and xz, see the comparative example of a TAR package of a website backup file:
site.tar 9,8M # uncompressed file;
site.tar.gz 2,6M # file compressed with gzip
site.tar.bz2 2,4M # file compressed with bzip
site.tar.xz 2,1M # file compressed with xz
Conclusion
If you need to backup data on Linux, it is essential to know the tar, gzip, bzip and xz commands.
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.
Like it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.