A common task on most modern operating systems is to combine and compress multiple files into a single file. This could be in order to store files on a smaller device, to make it easy to download files from a website, or to merge and compress files for email transport. This guide focuses on some of the more common Linux utilities that merge and compress files.

Tar

The purpose of the tar command, which stands for tape archive, is to merge multiple files into a single file. To create a tar file named sample.tar, execute the following

#tar -cf sample.tar <files_to_merge>

To list the contents of a .tar file:

#tar -tf sample.tar

To extract the contents of a .tar file:

#tar -xf sample.tar

d

gzip

Use the gzip command to compress files:

#gzip  <filename>

To display percentage of compression,

#gzip -v <filename>

To decompress the file,

#gzip -d <filename>

Note that the gzip command replaces the original file with the smaller compressed file.

gunzip

Use the gunzip command to decompress gzipped files

#gunzip <filename>

bzip2

Use the bzip2 command to compress files:

#bzip2 <filename>

To Display percentage of compression.

#bzip2 -v <filename>

To Decompress the file

#bzip2 -d <filename>

Note that the bzip2 command replaces the original file with the compressed file.

Xz

Use the xz command to compress files:

#xz  <filename>

You can  use the unxz command to decompress the file

#unxz <filename>

To display percentage of compression,

#xz -v <filename>

You can also write output to STDOUT and do not replace the original file. Use redirection to place output data into a new file

#xz -c file2 > file2.xz

The gzip, xz, and bzip2 commands are very similar. The biggest difference is the technique used to compress files. The gzip command uses the Lempel-Ziv (LZ77) coding method, whereas the bzip2 command uses the Burrows-Wheeler (BWT) block-sorting text-compression algorithm and Huffman coding. The xz command uses the LZMA and LZMA2 compression methods.

The following two tabs change content below.
Ruwantha Nissanka is a Professional Cyber Security Engineer from Sri lanka with having a demonstrated history of providing cyber security services for multiple organizations in Sri Lanka. He is a positive person who wants to believe the best in others and he likes to help, encourage people and make them feel good.