There are some utilities for compression and decompression on Linux that are used often. Now I’ll explain their usages one by one.

Suppose we have files a.txt, b.txt, c.txt and folders aa, bb, cc in the current folder.

  • bzip2 There are two styles of using bzip2 Style 1 (recommended)

    bzip2 -z a.txt
    bzip2 -d a.txt.bz2
    

    Style 2

    bzip2 a.txt
    bunzip2 a.txt.bz2
    

    Note. bunzip2 = bzip2 –decompress. By default, the original file will be deleted. If you want to keep the original file, add -k.

    bzip2 -zk a.txt
    bzip2 -dk a.txt.bz2
    
  • xz xz is very similar to bzip2. They are almost the same in usage. Style 1 (recommended)

    xz -z a.txt
    xz -d a.txt.xz
    

    Style 2

    xz a.txt
    unxz a.txt.xz
    

    Note. unxz = xz –decompress If you want to keep the original file, add -k

    xz -zk a.txt
    xz -dk a.txt.xz
    
  • lzma lzma is very similar to xz. They are almost the same in usage. Style 1 (recommended)

    lzma -z a.txt
    lzma -d a.txt.lzma
    

    Style 2

    lzma a.txt
    unlzma a.txt.lzma
    

    Note. unlzma = lzma –decompress If you want to keep the original file, add -k

    lzma -zk a.txt
    lzma -dk a.txt.lzma
    
  • gzip gzip has a little difference in usage from bzip2, xz and lzma. gzip doesn’t have -z option. So if you want to compress, you don’t add anything. Style 1 (recommended)

    gzip a.txt
    gzip -d a.txt.gz
    

    Style 2

    gzip a.txt
    gunzip a.txt.gz
    

    The way of keeping the original file is also different. gzip doesn’t have -k option, but it provides -c option for both compression and decompression, which writes on standard output and keeps original file unchanged.(bzip2, xz, lzma only provides -c option for decompression.) Thus we can use

    gzip -c a.txt > a.txt.gz
    gzip -dc a.txt.gz > a.txt
    

    to keep the original file.

  • zip zip can compress a batch of files into a single .zip file, while maintaining their folder structure. The basic command line looks like

    zip [options] out_file in_file1 in_file2 ...
    

    Examples

    zip out.zip a.txt b.txt c.txt #compress files a.txt, b.txt, c.txt into out.zip
    zip -r out.zip aa bb cc #compress folders aa, bb, cc into out.zip, -r means recursively
    

    Use unzip for decompression

    unzip out.zip #decompressed into the current folder
    unzip -d out out.zip #decompressed into folder out/, which is created if not exists
    
  • 7z 7z can also compress and decompress a batch of files and keep their folder structure. It uses a for compression and x for decompression.

    7z a out.7z a.txt b.txt c.txt
    7z a out.7z aa bb cc #automatically recursive for folders
    7z x out.7z #decompressed into the current folder
    7z x -oout out.7z #decompressed into folder out/, which is created if not exists
    

    Note. Don’t add spaces after -o option.

  • rar rar can also compress and decompress a batch of files and keep their folder structure. It uses a for compression and x for decompression.

    rar a out.rar a.txt b.txt c.txt
    rar a out.rar aa bb cc #automatically recursive for folders
    rar x out.rar #decompressed into the current folder
    rar x out.rar out/ #decompressed into folder out/, which is created if not exists
    

    Note. The backslash / at the end of the output folder cannot be omitted. The decompression can also be done using unrar

    unrar x out.rar #decompressed into the current folder
    unrar x out.rar out/ #decompressed into folder out/, which is created if not exists
    

    Note. The backslash / at the end of the output folder cannot be omitted.

  • tar Strictly speaking, tar is not a compression/decompression tool, but an archiving tool. To use tar, first get to know the following options

    -c, create an archive
    -x, extract from an archive
    -v, verbose
    -f file, set the archive to file
    -C DIR, change to directory to DIR (all operations are based on that directory)
    -z, gzip
    -j, bzip2
    -J, xz
    --lzma, lzma
    

    Examples

    tar -cvf out.tar a.txt b.txt c.txt
    tar -cvf out.tar aa bb cc #automatically recursive for folders
    tar -xvf out.tar #decompressed into the current folder
    tar -zcvf out.tar.gz aa bb cc #using gzip compression
    tar -zxvf out.tar.gz -C out #decompressed into folder out/, which is NOT automatically created