Skip to content

File Compression


When sharing files with other people, you may need to package and compress them.

There are very good tools for them:

  • gzip
  • bzip2
  • xz
  • tar
  • zip

Package and compress the contents of a directory (including subdirs):

Terminal window
tar -czvf archive.tar.gz /tmp/testdir

Decompress and unpackage the archive:

Terminal window
tar -xzvf archive.tar.gz

All commands are tested on Debian 12 & 13 and Ubuntu Server 22.04 & 24.04 LTS

Create a temporary test directory

Terminal window
mkdir /tmp/testdir

Create 3 more directories d1, d2, d3

Terminal window
mkdir /tmp/testdir/d{1..3}

Create 3 files at each directory

Terminal window
touch /tmp/testdir/d1/f1{1..3}
touch /tmp/testdir/d2/f2{1..3}
touch /tmp/testdir/d3/f3{1..3}

Fill the files with random data with the given size

Terminal window
< /dev/urandom tr -dc "[:space:][:print:]" | head -c1M > /tmp/testdir/d1/f11
< /dev/urandom tr -dc "[:space:][:print:]" | head -c2M > /tmp/testdir/d1/f12
< /dev/urandom tr -dc "[:space:][:print:]" | head -c3M > /tmp/testdir/d1/f13
< /dev/urandom tr -dc "[:space:][:print:]" | head -c4M > /tmp/testdir/d2/f21
< /dev/urandom tr -dc "[:space:][:print:]" | head -c5M > /tmp/testdir/d2/f22
< /dev/urandom tr -dc "[:space:][:print:]" | head -c6M > /tmp/testdir/d2/f23
< /dev/urandom tr -dc "[:space:][:print:]" | head -c7M > /tmp/testdir/d3/f31
< /dev/urandom tr -dc "[:space:][:print:]" | head -c8M > /tmp/testdir/d3/f32
< /dev/urandom tr -dc "[:space:][:print:]" | head -c9M > /tmp/testdir/d3/f33

Final tree

├── d1
│   ├── f11
│   ├── f12
│   └── f13
├── d2
│   ├── f21
│   ├── f22
│   └── f23
└── d3
├── f31
├── f32
└── f33


Installation (Most probably, it is already installed).

Terminal window
sudo apt update
sudo apt install gzip

gzip compresses (and decompresses) only 1 file, it is not used to prepare a package of files. But it can be combined with tar to make a compressed package of files.

Compress a file
(Original file is removed, a new file is created there with .gz extension)

Terminal window
gzip /tmp/testdir/d1/f11

Decompress a file
(.gz file is removed, a new file is created there without .gz extension)

Terminal window
gzip -d /tmp/testdir/d1/f11.gz
gunzip /tmp/testdir/d1/f11.gz

Compress a file, keep the original file

Terminal window
gzip -k /tmp/testdir/d1/f11

Decompress a file, keep the .gz file

Terminal window
gzip -kd /tmp/testdir/d1/f11.gz

Compress all files in a directory
(All files are removed, new files are created there with .gz extensions)

Terminal window
gzip /tmp/testdir/d1/*

Decompress all files in a directory

Terminal window
gzip -d /tmp/testdir/d1/*
gunzip /tmp/testdir/d1/*

Compress all files recursively (including subdirectories)

Terminal window
gzip -r /tmp/testdir

Decompress all files recursively (including subdirectories)

Terminal window
gzip -rd /tmp/testdir

Some useful options:

  • -l —list: List compressed file contents
  • -q —quiet: Suppress all warnings
  • -1 —fast: Compress faster
  • -9 —best: Compress better


Installation (Most probably, it is already installed).

Terminal window
sudo apt update
sudo apt install bzip2

bzip2 compresses (and decompresses) only 1 file, it is not used to prepare a package of files. But it can be combined with tar to make a compressed package of files.

Provides higher compression ratios compared to gzip, but may be slower.

Compress a file
(Original file is removed, a new file is created there with .bz2 extension)

Terminal window
bzip2 /tmp/testdir/d1/f11

Decompress a file
(.bz2 file is removed, a new file is created there without .bz2 extension)

Terminal window
bzip2 -d /tmp/testdir/d1/f11.bz2
bunzip2 /tmp/testdir/d1/f11.bz2

Compress a file, keep the original file

Terminal window
bzip2 -k /tmp/testdir/d1/f11

Decompress a file, keep the .bz2 file

Terminal window
bzip2 -kd /tmp/testdir/d1/f11.bz2

Compress all files in a directory
(All files are removed, new files are created there with .bz2 extensions)

Terminal window
bzip2 /tmp/testdir/d1/*

Decompress all files in a directory

Terminal window
bzip2 -d /tmp/testdir/d1/*
bunzip2 /tmp/testdir/d1/*

Some useful options:

  • -q —quiet: Suppress all warnings
  • -1 —fast: Compress faster
  • -9 —best: Compress better


Installation (Most probably, it is already installed).

Terminal window
sudo apt update
sudo apt install xz-utils

xz compresses (and decompresses) only 1 file, it is not used to prepare a package of files. But it can be combined with tar to make a compressed package of files.

Compress a file
(Original file is removed, a new file is created there with .xz extension)

Terminal window
xz /tmp/testdir/d1/f11

Decompress a file
(.xz file is removed, a new file is created there without .xz extension)

Terminal window
xz -d /tmp/testdir/d1/f11.xz
unxz /tmp/testdir/d1/f11.xz

Compress a file, keep the original file

Terminal window
xz -k /tmp/testdir/d1/f11

Decompress a file, keep the .xz file

Terminal window
xz -kd /tmp/testdir/d1/f11.xz

Compress all files in a directory
(All files are removed, new files are created there with .xz extensions)

Terminal window
xz /tmp/testdir/d1/*

Decompress all files in a directory

Terminal window
xz -d /tmp/testdir/d1/*
unxz /tmp/testdir/d1/*

Some useful options:

  • -l —list: List compressed file contents
  • -q —quiet: Suppress all warnings
  • -0: Compress faster
  • -9: Compress better


Installation (Most probably, it is already installed)

Terminal window
sudo apt update
sudo apt install tar

tar actually is an archiving utility, it is used to package files. It can package many files to a file. It can also transparently be combined with gzip bzip2 and xz tools to compress files.

Package and compress (with gzip) the contents of a directory (including subdirectories).
Archive file is created in the current directory.

Terminal window
tar -czvf archive.tar.gz /tmp/testdir

Decompress and unpackage the archive
Creates the directory structure under current directory

Terminal window
tar -xzvf archive.tar.gz

(package) compress & decompress (unpackage) with bzip2

Terminal window
tar -cjvf archive.tar.bz2 /tmp/testdir
tar -xjvf archive.tar.bz2

(package) compress & decompress (unpackage) with xz

Terminal window
tar -cJvf archive.tar.xz /tmp/testdir
tar -xJvf archive.tar.xz

See the contents of an archive

Terminal window
tar -tvf archive.tar.gz
tar -tvf archive.tar.bz2
tar -tvf archive.tar.xz

Extract files to another directory
Directory must exist.

Terminal window
tar -xzvf archive.tar.gz -C /tmp
tar -xjvf archive.tar.bz2 -C /tmp
tar -xJvf archive.tar.xz -C /tmp

4.3. Usage Without Compression (Archiving)

Section titled “4.3. Usage Without Compression (Archiving)”

Tar can be used without compressing too, actually it is the main usage of the tar command.

All the conditions apply in 4.2.1., if you skip compression options (-z, -j, -J) then you get an archive without compression. This time you can add files to it, or remove files from it.

Archive a directory (and subdirectories)

Terminal window
tar -cvf archive.tar /tmp/testdir

List the contents

Terminal window
tar -tvf archive.tar

Unarchive it

Terminal window
tar -xvf archive.tar

Add a file to the archive

Terminal window
tar -rvf archive.tar test.txt

Remove a file from the archive

Terminal window
tar -vf archive.tar --delete test.txt

Tar has tons of options. You can list them all by:

Terminal window
tar --help

Some selected options:

  • -c: Create a new archive.
  • -t: List the contents of the archive.
  • -x: Extract files from the archive.
  • -v: Verbose mode (show the progress and file names).
  • -f: Specify the archive file name.
  • -z: Use gzip compression.
  • -j: Use bzip2 compression.
  • -J: Use xz compression.
  • -C: Change to the specified directory before extracting.
  • -r: Append files to the end of an archive
  • -a: use archive suffix to determine the compression program.
  • —delete: delete from the archive
  • —exclude=PATTERN: exclude files, given as a PATTERN
  • —overwrite: overwrite existing files when extracting
  • —remove-files: remove files after adding them to the archive


Although gzip, bzip2, xz, and tar is more than enough for compression and archiving; sometimes you may need to exchange files with the unlucky people using Wind*ws. zip tool may be helpful then.

Installation

Terminal window
sudo apt update
sudo apt install zip

Compress a file (Zip file is created in the current dir, original file stays there)

Terminal window
zip f11.zip /tmp/testdir/d1/f11

Decompress a file (Decompress to the current dir by creating the directory structure in zip file, zip file stays where it is).

Terminal window
unzip f11.zip

Compress all the files in a directory

Terminal window
zip d1.zip /tmp/testdir/d1/*

Compress all the files in a directory including subdirectories

Terminal window
zip -r test.zip /tmp/testdir/

Compress and encrypt

Terminal window
zip -er test.zip /tmp/testdir/

List the contents of a zip file

Terminal window
unzip -l test.zip

Add new files to a zip file

Terminal window
zip -u d1.zip /tmp/testdir/d2/*

Some options:

  • -u: update; only changed or new files
  • -d: delete entries in zipfile
  • -m: move into zipfile (delete OS files)
  • -r: recurse into directories
  • -j: junk (don’t record) directory names
  • -1: compress faster
  • -9: compress better
  • -q: quiet operation
  • -v: verbose operation/print version info
  • -x: exclude the following names
  • -i: include only the following names
  • -e: encrypt