How To Compress And Decompress Files In Linux Compression How to Compress and decompress files in Linux...Compressing is a very useful process when backing up important files as well as sending large files over the internet.Remember that compressing an already compressed file adds extra overhead, hence, you will receive a slightly bigger file. For this reason, do not compress a compressed filed. There are lots of programs that you can use to compress and decompress files in GNU/Linux. In this tutorial, we will teach you about two applications only. Compress and decompress files in Linux The most common programs that are used to compress files in Unix-like systems are: gzip bzip2 Compress and decompress files using Gzip program ‘gzip’ is a utility which will help you compress and decompress files with ‘Lemepel-Ziv’ coding (LZ277 algorithm. 1.1 Compress files To compress a file called ‘dreamvps.txt’, replacing it with a gzipped compressed version, execute the following command. $ gzip dreamvps.txt Gzip should replace the original file ‘dreamvps.txt’ with a gzipped compressed version called ‘dreamvps.txt.gz'.You can also use the ‘gzip’ command in different ways.One good example is that we can create a compressed version of a particular command’s output. Look at the command below. $ ls -l Downloads/ | gzip > dreamvps.txt.gz The command above will create a compressed version of the directory listing of Downloads folder. 1.2 Compress files and write the output to different files (Don’t replace the original file) By default, the ‘gzip’ program will compress any given file, replacing it with a gzipped compressed version. You can, however, keep the original file and enter the output to standard output. For instance, the command below will compress ‘dreamvps.txt’ and will also enter the output to ‘output.txt.gz’. $ gzip -c dreamvps.txt > output.txt.gz Similarly, to decompress a gzipped file specifying the output filename. $ gzip -c -d output.txt.gz > dreamvps1.txt The command above will decompress the ‘output.txt.gz' file and will enter the output to ‘dreamvps1.txt’ file. In both cases, it will not delete the original file. 1.3 Decompress files In order to decompress the file ‘dreamvps.txt.gz’, you will need to replace it with the original uncompressed version; our example will use the following. $ gzip -d dreamvps.txt.gz You can also use gunzip to decompress the files. $ gunzip dreamvps.txt.gz 1.4 View contents of compressed files without decompressing them If you want to view the contents of the compressed file using gzip with out decompressing it, use ‘–c’ flag like shown below. $ gunzip -c dreamvps1.txt.gz Alternatively, you can also use the ‘zcat’ utility like in the following. $ zcat dreamvps.txt.gz You could also pipe the output to ‘less’ command to view the output page by page like below. $ gunzip -c dreamvps1.txt.gz | less $ zcat dreamvps.txt.gz | less Alternatively, there is a ‘zless’ program which will do the same function as the pipeline above. $ zless dreamvps1.txt.gz 1.5 Compress file with gzip by specifying compression level Another notable advantage of ‘gzip’ is; it will support compression level. It will support 3 compression levels like shown below: 1– Fastest (Worst) 9– Slowest (Best) 6– Default level To compress a file called ‘dreamvps.txt’, you will need to replace it with a gzipped compressed version with ‘best’ compression level, you will need to use the following. $ gzip -9 dreamvps.txt 1.6 Concatenate multiple compressed files It is also possible to concatenate several compressed files into one. You can see how by looking at the examples below. $ gzip -c dreamvps1.txt > output.txt.gz $ gzip -c dreamvps2.txt >> output.txt.gz The two commands above will compress ‘dreamvps1.txt’ and ‘dreamvps2.txt’ then will save them in one file called ‘output.txt.gz’.You may view the contents of both files (‘dreamvps1.txt’ and ‘dreamvps2.txt’) without extracting them using any one of the following commands. $ gunzip -c output.txt.gz $ gunzip -c output.txt $ zcat output.txt.gz $ zcat output.txt The ‘bzip2’ is quite similar to the gzip program, however, it uses a different compression algorithm called the ‘Burrow-Wheeler block sorting text compression’ algorithm, and Huffman coding. The files compressed using ‘bzip2’ will end with a ‘.bz2’ extension.As we have stated, the usage of ‘bzip2’ is almost the exact same as gzip. Simply replace ‘gzip’ in the example above with ‘bzip2’, ‘gunzip’ with ‘bunzip2’, ‘zcat’ with ‘bzcat' and so on.To compress a file with ‘bzip2’, replace it with a compressed version, run the below. $ man gzip In the case that you do not want to replace the original file, use the ‘–c’ flag and enter the output to a new file. $ bzip2 dreamvps.txt To decompress a compressed file, use the below. $ bzip2 -c dreamvps.txt > output.txt.bz2 Or the below. $ bzip2 -d dreamvps.txt.bz2 To view the contents of a compressed file without having to decompresses it, the below. $ bunzip2 -c dreamvps.txt.bz2 Or the below. $ bzcat dreamvps.txt.bz2 For more details, refer man pages. $ man bzip2 Final Thoughts: In this tutorial, we have taught you about gzip and bzip2 programs and how to use them to compress and decompress files with some examples in GNU/Linux. Cheers!