How to Compress/Uncompress Files in Linux Using gzip, bzip2, 7z, rar and zip

Compress/uncompress files are frequent operations. The normal tools for compressing/uncompressing in Linux is gzip, bzip2, 7z, rar and zip. This post introduces how to compress and uncompress file in Linux using these tools. We use best compressing rate with all these tools and mark the options for “best rate” in bold fonts. We can delete these options if we don’t so care about the size of the compressed file and want a faster compressing.

The example we use here is to compress a directory dir recursively into a compressed file with particular file extension which specifies the compressing method.

gzip

Table of Contents

Compress

gzip -r --best -c dir > ./dir.gz

Uncompress

gunzip ./dir.gz

bzip2

Compress

tar -c dir | bzip2 --best -c > ./dir.tar.bz2

Uncompress

tar xf ./dir.tar.bz2

7z

Compress

7za -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on a ./dir.7z dir

Uncompress

7za x ./dir.7z

RAR

Compress

rar -m5 a ./dir.rar dir

Uncompress

rar x ./dir.rar

ZIP

Compress

zip -r -9 ./dir.zip dir

Uncompress

unzip ./dir.zip

We can delete the options in black fonts for faster compressing and shorter command line instead of best compressing rate.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *