How to compress/decompress files in Linux

This guide will give you detail explanation of various types of file
compression and decompression methods in Linux. How-to to compress/decompress
the various file types present in Linux. The first thing that we
must know, it’s to Compress/Decompress we need some applications, like
“unzip” or “unrar”. But here to compress/decompress we’ll make use of
some commands.

This guide explains How to Compress/Decompress files in following types of compress files using command line:

  • tar Files
  • gz Files
  • bz2 Files
  • tar.gz Files
  • tar.bz2 Files
  • zip Files
  • rar Files

tar Files

To compress files in .tar, use the following command:

Code:
tar cvf file.tar / dir/ a/*

-c : Tells tar to create an archive.

-v : Tells tar to show what is packaging.

-f : Tells tar that the next argument is the name of file.tar.

To decompress the .Tar files, use the following command:

Code:
	tar xvf file.tar

-x : Tells tar to decompress the file.tar.

-v : Tells tar to show what is unpacking.

-f : Tells tar that the next argument is the filename to unpack.

If you want to view the contents of a file. Tar, use the following command:

Code:
tar -tf file.tar

-t : List the file .Tar contents

-f : Tells tar that the next argument is the filename to view.

gz Files

To compress files in .Gz, use the following command:

Code:
gzip -9 file

-9 : Gz tells you use the largest possible compression factor.

To decompress files. Gz, use the following command:

Code:
gzip -d file.gz

-d : Indicates decompression

bz2 Files

To compress files in bz2 format, use the following command:

Code:
bzip file

To decompress .Bz2 files , use the following command:

Code:
bzip2 -d file.bz2

-d: indicates decompression.

Note: Both the
compressor gzip as bzip2, only compressed files, not directories, to
compress directories (folders) must be used in combination with tar.

tar.gz Files

To compress files in tar.gz format, use the following command:

Code:
tar -czfv file.tar.gz files

-c : Tells tar to create archive.

-z : Indicates that using the gzip compressor

-f : Tells tar that the next argument is the name of file.tar

-v : Tells tar to show what packaging will

To unzip files with extension tar.gz, use the following command:

Code:
tar -xzvf file.tar.gz

-x : Tells tar to extract the tar.gz file contents

-z : Tells tar that’s gzipped

-v : He shows the contents of the file

-f : Tells tar that the next argument is the file to decompress.

To view the contents of a tar.gz compressed file, use the following command:

Code:
tar -tzf file.tar.gz

tar.bz2 Files

To compress in tar.bz2,
we use the pipeline (|) parameter, which is "filter" that makes the
output of a command to the input of another, as in this example: tar
packages the various files or directories and passes it to the exit
bzip2 to compress this and the result of this, I redirected (>) to
the final file tar.bz2

Code:
tar -c  file1 | bzip2 > file.tar.bz2

To uncompress tar.bz2 files, use the following command:

Code:
bzip2 -dc file.tar.bz2 | tar -xv


And to see the contents of a tar.bz2 file, use the following command:

Code:
bzip2 -dc file.tar.bz2 | tar -t

zip Files

To compress in zip format, it will use the following command:

Code:
zip file.zip file

To decompress zip files, use the following command:

Code:
    unzip file.zip

To view the contents of a zip file, put the following command:

Code:
    unzip-v file.zip

Files rar

To compress in rar format, will be used the following command:

Code:
rar -a file.rar file

To unzip files in zip format, use the following command:

Code:
unrar -x file.rar

To view the contents of a zip file, use the following command:

Code:
    unrar-v file.rar
Code:
     unrar-l file.rar

As i said this commands are really useful when you are in linux without
graphical environment and you have to unzip a file. Or its also useful
when you don’t want to waist time in installing any compression tool in
your system.

But for those unwilling to use the terminal, can rest assured that
there are very good user interfaces available to use, like File Roller.

LEAVE A REPLY

Please enter your comment!
Please enter your name here