File Compression & Decompression on Linux

One of the basic file operation performed while working on Linux is file compression and decompression. Compression reduces the size of the file depending on the type of the file. Reduced file size helps when moving files between different servers or also If you want to free some of your hard disk.

Here I have brought you the different commands with examples that are often used to compress and decompress the files on Linux.

Zip Compression

Zip does not come by default installed on Ubuntu so you may have to install it. Simply run sudo apt get install zip

Compress a file with Zip

In the following example, file.zip is the output file and file is the name of file that will be zipped. Zip keeps the input file.

$ zip  file.zip  file  

Compress a Directory with Zip

you need to use -r option to recursively compress the files of a directory.

$ zip  -r  directory.zip  directory

Decompress a File or a Directory with Zip

Unzip is the command that you can use to decompress the files compressed by zip.

$ unzip    file.zip
$ unzip    directory.zip

Gzip Compression

Compress a File with Gzip

Unlike Zip we don’t need to specify the output file here. But it will not keep the input file. To keep input file specify option -k

$ gzip  file
   output will be:  file.gz

Compress a Directory with Gzip

we can’t directory compress a directory with gzip. But we can do it via tar first we need to make the tar ball of the directory and then compress it with gzip.

$ tar  cvf   directory.tar   directory          (c = create, v = verbose, f = file)
   output will be:  directory.tar  file.

$ gzip  directory.tar
   output will be:  directory.tar.gz

Decompress a File or a Directory with Gzip

Gunzip is the command to decompress the files compressed by gzip.

$ gunzip  file.gz

To decompress a tar ball compress with gzip run following command.

$  tar xvfz   directory.tar.gz

x  = extract
v = verbose
f = file
z = gunzip (gzip decompression)

OR

you can do it in two steps.

$ gunzip  directory.tar.gz
   output will be:   directory.tar

$ tar  xvf   directory.tar


Bzip2 Compression

Compress a File with Bzip2

Like gzip it will also not save the input file to keep input file we need to specify -k option. And it is also designed to work with only files not directories.

$ bzip2   file
   output will be:  file.bz2

Compress a Directory with Bzip2

Same as gzip bzip2 is not for directories but we can make it work with directories by making tar ball of the directory.

$ tar  cvf  directory.tar  directory
output will be:   directory.tar  file.

$ bzip2  directory.tar
output will be:  directory.tar.bz2

Decompress a File or a Directory with Bzip2

$ bunzip2   file.bz2

$ bunzip2  directory.tar.bz2

$ tar  xvf  directory.tar

These are the basic file compression tools that are normally  used on Linux systems. There is an another open-source file archiver tool 7-zip, to learn about that you can check my post on file compression with 7-zip.

I hope this information helped you. Let me know your thoughts in the comment section.


2,252 Replies to “File Compression & Decompression on Linux”