File archiving and compression in Linux

We can use zip and tar commands to archive and compress  the files in Linux.

Archiving and Compressing using zip command

You can use below command to zip file abc.txt into abc.zip
$ zip abc.zip abc.txt

You will get below error if you do not provide the .zip destination file.
zip error! nothing to do

To compress directory, you need to use -r option. It means zip files recursively.

Below command can be used to unzip the zipped files.
$ unzip abc.txt.zip

Below command will list the zipped files without extraction.
$ unzip -l abc.txt.zip

Compressing files using gzip command

gzip uses deflat algorithm to compress data.
Below command will compress the abc.txt file to abc.txt.gz
$ gzip abc.txt

To compress directory, you need to use -r option. It means zip files recursively.

To uncompress any .gz file, you can use below command. Note that gunzip is an alias for below command.

$ gzip -d <file-name>.gz

Note that zcat is an alias for below command.

$ gunzip -c <file-name>.gz

Compressing files using bzip2 command
bzip2 uses Burrows-Wheeler algorithm which is less fast as compared to gzip but it makes more compact files than gzip.

To compress the file using bzip2, you need to use below command.
$ bzip2 abc.txt

To compress directory, you need to use -r option. It means zip files recursively.

To uncompress any *.bz2 file, you can use below command.
bzip2 -d <file-name>.bz2

Compressing file using xz command
xz compression uses LZMA2 algorithm. Remember that 7zip also uses the same algorithm.

Below command can be used to compress file using xz utility
$ xz <file-name>

Below command can be used to list the details of compression
$ xz -l <file-name>.xz

Below command can be used to uncompress the .xz file.
$ xz -d <file-name>.xz

Archiving and Compressing using tar command

In below example we are using tar command to archive and compress mybig-direcotry using basic algorithm. Here -c stands for create new archive and -x stands for extract compressed files. f stands for file.

$ tar -cvf mytarball.tar mybig-direcotry
$ tar -xvf mytarball.tar

In below example we are using tar command to archive and compress mybig-direcotry using gz algorithm.

$ tar -czvf mytarball.tar.gz mybig-direcotry
$ tar -xzvf mytarball.tar.gz

In below example we are using tar command to archive and compress mybig-direcotry using bz2 algorithm.

$ tar -cjvf mytarball.tar.bz2 mybig-direcotry
$ tar -xjvf mytarball.tar.bz2

In below example we are using tar command to archive and compress mybig-direcotry using xz algorithm.

$ tar -cJvf mytarball.tar.xz mybig-direcotry
$ tar -xJvf mytarball.tar.xz

 

You may also like...