This page looks best with JavaScript enabled

Archiving and compression on GNU/Linux

 ·  ☕ 4 min read

The traditional Unix archiving and compression tools are separated according to the Unix philosophy:

  • A file archiver combines several files into one archive file, e.g. tar.
  • A compression tool compresses and decompresses data, e.g. gzip.
    These tools are often used in sequence by firstly creating an archive file and then compressing it.
    Of course there are also tools that do both, which tend to additionally offer encryption, error detection and recovery.

tar

tar is a utility for collecting many files into one archive file, often referred to as a tarball. tar also can work with a lot of compessions. Here’s how to use it:

  • Create archive.tar from files file1 and file2:

    tar -cf archive.tar file1 file2
    
  • Extract all files form archive.tar:

    tar -xf archive.tar
    
  • Extract all files form archive.tar to specific directory:

    tar -xf archive.tar -C /home/$USER/Documents
    
  • Extract all .jpg files form archive.tar using wildcards:

    tar -xf archive.tar --wildcards "*.jpg"
    
  • List all files in archive.tar:

    tar -tf archive.tar
    
  • Add file(s) to existing archive:

    tar -rf archive.tar file3
    
  • Extract specific file:

    tar -xf archive.tar file1
    
  • Create compressed archive archive.tar.gz:

    tar -czf archive.tar.gz file1 file2
    
  • Extract all files from compressed archive archive.tar.gz:

    tar -xzf archive.tar.gz
    
  • View all options:

    man tar
    

Additional options:

  • -v - verbose output.
  • -O - write to stdout.
  • -j or --bzip2 - filter the archive through bzip2.
  • -J or --xz - filter the archive through xz.
  • --lzip - filter the archive through lzip.
  • --lzma - filter the archive through lzma.
  • --lzop - filter the archive through lzop.
  • -z or --gzip - filter the archive through gzip.
  • --zstd - filter the archive through zstd.

gzip

The gzip command is a common way of compressing files within Linux. Here’s how to use it:

  • Compress a file:

    gzip file
    
  • Compress a file, while specifying the compression level (1 being worst, 9 best, and 6 default). For example, level 7:

    gzip -7 file
    
  • Decompress a file:

    gzip -d file.gz
    
  • List compressed file contents:

    gzip -l file.gz
    
  • View all options:

    man gzip
    

Additional options:

  • -v - verbose output.
  • - write to stdout.
  • -k - keep source file.
  • -f - force overwrite of output file and compress links.
  • -t - test compressed file integrity.

lz4

LZ4 is a lossless data compression algorithm that is focused on compression and decompression speed. Here’s how to use lz4 compression tool:

  • Compress a file:

    lz4 file
    
  • Compress a file, while specifying the compression level (1 being worst, 9 best, and 3 default). For example, level 7:

    lz4 -7 file
    
  • Decompress a file:

    lz4 -d file.gz
    
  • View all options:

    man lz4
    

Additional options:

  • -v - verbose output.
  • - write to stdout.
  • --rm - remove source file.
  • -f - force overwrite of output file.
  • -t - test compressed file integrity.

xz

xz is a general-purpose data compression tool. Here’s how to use it:

  • Compress a file:

    xz file
    
  • Compress a file, while specifying the compression level (0 being worst, 9 best, and 6 default). For example, level 7:

    xz -7 file
    
  • Decompress a file:

    xz -d file.gz
    
  • List compressed file contents:

    xz -l file.gz
    
  • View all options:

    man xz
    

Additional options:

  • -v - verbose output.
  • - write to stdout.
  • -k - keep source file.
  • -f - force overwrite of output file.
  • -t - test compressed file integrity.

bzip2

bzip2 is a file compression program that uses the Burrows–Wheeler algorithm. Here’s how to use it:

  • Compress a file:

    bzip2 file
    
  • Compress a file, while specifying the compression level (1 being worst, 9 best, and 9 default). For example, level 7:

    bzip2 -7 file
    
  • Decompress a file:

    bzip2 -d file.gz
    
  • View all options:

    man bzip2
    

Additional options:

  • -v - verbose output.
  • - write to stdout.
  • -k - keep source file.
  • -f - force overwrite of output file.
  • -t - test compressed file integrity.

zstd

You can read about ZStandard on my blog here.

zip/unzip

You can read about zip and unzip tools on my blog here.

rar/unrar

You can read about rar and unrar tools on my blog here.


Vladislav Pashinskikh
WRITTEN BY
Vladislav Pashinskikh
DevOps Engineer, GNU/Linux enthusiast, FOSS and privacy activist from Ukraine