×

Search anything:

Compression and decompression in Linux

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

In this article we discuss various Linux tools used for compression and decompression such as zip, tar, gzip, bzip2, xz. We cover commands used to compress and decompress files and directories.

Table of contents.

  1. Introduction.
  2. zip.
  3. tar
  4. gzip
  5. bzip2
  6. xz
  7. Summary.
  8. References.

Introduction.

Compression is useful e.g when making backups, sending large files over the internet etc.
The commonly used programs for compression in Linux are, gzip, bzip2, zip, tar, xz.

zip.

To compress a file with zip we write,

zip largeFile.zip largeFile

After executing this command, a file largeFile.zip will be created in the current directory.

To compress a directory we write,

zip largeDir.zip largeDir

We can also compress a file and password-protect it using the -P option as follows,

zip -P 1234 largeFile.zip largeFile

where 1234 is the password we use to lock the compressed file.

Now when we want to decompress it, we will be prompted to enter the password.
However, this is not safe since by using the history command one can easily read the password used.

To solve this we use the -e option as follows,

zip -e largeFile.zip largeFile

To decompress a file we write,

unzip largeFile.zip

To decompress a directory we write,

unzip largeDir.zip

tar.

To compress a file largeFile with tar, we write,

tar cfz largeFile.tgz largeFile

The result is a compressed version of largeFile with the extension .tgz

To compress a directory largeDIR write,

tar cfz largeDIR.tgz largeDIR

To decompress a file we write,

tar xf largeFile.tgz

To decompress a directory we write,

tar xf largeDIR.tgz

gzip.

To compress a file with gzip we write,

gzip largeFile

If no errors are encountered a compressed version of largeFile, largeFile.gz is created in the current directory.

To decompress we write,

gunzip largeFile.gz

bzip2.

This command compresses a file in place and therefore will replace the original file with a compressed version.
To compress a file largeFile, we write,

bzip2 largeFile

After the compression the original file is replaced with the compressed version largeFile.bz2.

To decompress we write,

bunzip2 largeFile.bz2

xz.

This command compresses a file and replaces the original with the compressed version.
To compress a file largeFile, write

xz largeFile

After the operation largeFile is replaced with its compressed version largeFile.xz.

To decompress we write,

xz -d largeFile.xz

We can also decompress it by writing,

unxz largeFile.xz

Question.

  1. After zipping a file with zip one can still view its contents by writing unzip -l largeFile.zip command, can you think of a way to prevent this?

Summary.

Compressing already compressed file will result in a slightly bigger file.
When you compare the file sizes of the compressed versions of a single file compressed with the discussed tools, you will find that the version compressed with xz is the smallest and therefore it wins in such cases.

In terms of runtime for compressing the file, you will notice that xz is the slowest while gzip is the fastest.

bzip2, gzip and xz all replace the original file with a compressed version.
tar and zip create a new compressed version of the file.

References.

  1. Execute man command where command is the command itself for its manual page or command --help to view command options.
Compression and decompression in Linux
Share this