Skip to content

Linux File Compactors

File compressors in Linux are used in the daily lives of administrators.

Learn about tar commands and gzip, bgzip, and xz compressors for joining and compressing files on Linux. Essential for any administrator.

Tar command

Use:

$ tar [ctxurGjzpvfNcmf] [destination] [source]

The name of this command comes from “Tape-Archive”. It reads files and directories and saves 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 takes two arguments. They are:

[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 it is used to restore the 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 the files from the .tar archive;
  • -u: Add more files to the .tar file only if they are new or modified;
  • -r: Add the files specified at the end of the file .tar;
  • -g: Create an incremental backup;
  • -j: Uses bzip2 to compress and unzip the files .tar;
  • -z: Uses gzip to compress and unzip the files .tar;
  • -p: Extracts files with the same creation permissions;
  • -v: Lists all processed files;
  • -f: Indicates that the destination is a disk file, not a magnetic tape drive;
  • -N: data Saves only files that are newer than the specified date;
  • -C: Specifies the location where the data will be extracted;
  • -M: Enables multiple volumes;
  • -T: Creates a .tar package from a list of files and directories.

Examples:

To save a particular /var/lib/mysql directory to a node /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/

To create a backup of directory /home/documents on multiple floppy disks:

# tar CVMf /dev/fd0 /home/documents

To create a package from a directory list in a The file is dated 01/05/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 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

Data backup operations can be automated through shell scripts programmed in cron, requiring only the administrator to exchange of media and review of reports.

Data Compressors

Gzip and gunzip command

For greater efficiency and savings of backup media, there is data compression feature.

The first data compressor Gzip is widely used. It uses a comprehension algorithm called Lempel-Ziv. This technique finds duplicate characters in the input data. THE The second occurrence of characters is replaced by pointers to the reference previous, in the form of pairs of distance and length. When compressing a file, gzip adds the suffix .gz.

To compress a file:

$ gzip file

To unzip a file:

$ gzip —d arquivo.gz

Or

$ gunzip arquivo.gz

bzip2 and bunzip2 command

The bzip2 compactor compresses files using the Burrows-Wheeler and Huffman algorithm. This technique operates on large blocks of data. The larger the size of the blocks, the greater the compression rate achieved. It is considered better than conventional compressors of data. When compressing a file, bzip2 adds the suffix .bz2.

To compress a file:

$ bzip2 file

To unzip a file:

$ bzip2 —d .bz2 file

Or

$ bunzip2 file. bz2

There are some cases where the file compressed may become larger than the original file. This may occur if the Algorithm used to find no occurrences to compress the data and the compactor header is added to the original file.

xz command

Also, we have the xz data compressor, which uses the algorithm similar to gzip. It produces files with the extension.xz or .lzma.

To compress a file:

$ xz file

For unzip:

$ xz —decompress.xz file

For you have an idea of the difference between the three compressors gzip, bzip2 and xz, See the comparative example of the TAR package from a website backup file:

site.tar 9.8M # uncompressed file;

site.tar.gz 2.6M # gzipped archive

site.tar.bz2 2.4M # file compressed with bzip

site.tar.xz 2.1M # file compressed with xz

Conclusion

If you need to back up 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 register 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