Archiving and Compression on Linux: An Introduction

Exploring the tar utility and standard compression tools on Linux

Image for post
Image for post

Introduction

Image for post
Image for post
Regular files being added to an archive with tar and then compressed with gzip.

The tar File Archiver

$ sudo pacman -S tar      # Arch
$ sudo apt install tar # Debian and Ubuntu
$ sudo dnf install tar # CentOS and Fedora

Compression Tools

$ sudo pacman -S gzip bzip2 xz zstd            # Arch
$ sudo apt install gzip bzip2 xz-utils zstd # Debian and Ubuntu
$ sudo dnf install gzip bzip2 xz zstd # Fedora
An overview of the standard Linux compression tools.

Environment Setup

Determining the Type of a File

$ file Acrylic.jpg# Output
Acrylic.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, progressive, precision 8, 5403x3602, components 3

Determining the Size of Files and Directories

# Find size of two JPEG files
$ du Acrylic.jpg ColdWarm.jpg
# Output
4516 Acrylic.jpg
3512 ColdWarm.jpg
$ du -hc Acrylic.jpg ColdWarm.jpg# Output
4.5M Acrylic.jpg
3.5M ColdWarm.jpg
7.9M total

Downloading Some Files to Work With

# Create a directory in the home folder and enter it
$ mkdir ~/archive-demos
$ cd ~/archive-demos
$ sudo pacman -S gnome-backgrounds
$ cp -fr /usr/share/backgrounds/gnome ~/archive-demos

An Archiving and Compression Tutorial

Frequently used options when using tar on the command line.
Tar options for filtering archives through the standard compression tools.
$ tar cfv wallpapers.tar gnome/
$ tar czfv wallpapers.tar.gz gnome/ # gzip
$ tar cjfv wallpapers.tar.bz2 gnome/ # bzip2
$ tar cJfv wallpapers.tar.xz gnome/ # xz
$ tar cfv wallpapers.tar.zst gnome/ --zstd # zstd
$ ls
$ tar tfv wallpapers.tar
$ tar tzfv wallpapers.tar.gz # gzip
$ tar tjfv wallpapers.tar.bz2 # bzip2
$ tar tJfv wallpapers.tar.xz # xz
$ tar tfv wallpapers.tar.zst --zstd # zstd
$ tar tzfv wallpapers.tar.gz gnome/Acrylic.jpg gnome/Wood.jpg
$ vim wallpapers.tar.xz
Image for post
Image for post
Listing the contents of the wallpapers.tar.xz archive with vim.
$ mkdir example-01
$ mv wallpapers.* example-01/
$ tar cfv wallpapers.tar -C gnome/ $(ls gnome/)
$ tar czfv wallpapers.tar.gz -C gnome/ $(ls gnome) # gzip
$ tar cjfv wallpapers.tar.bz2 -C gnome/ $(ls gnome) # bzip2
$ tar cJfv wallpapers.tar.xz -C gnome/ $(ls gnome) # xz
$ tar cfv wallpapers.tar.zst -C gnome/ $(ls gnome) --zstd # zstd
  # rename wallpapers.tar to prevent it being overwritten
$ mv wallpapers.tar wallpapers-original.tar
# decompress the .bz2 archive with -d flag
$ bzip2 -d wallpapers.tar.bz2
# delete Wood.jpg and Acrylic.jpg from the archive
$ tar f wallpapers.tar --delete Wood.jpg Acrylic.jpg
# compress the archive again with bzip2
$ bzip2 wallpapers.tar
# list content of the wallpapers.tar.bz2 archive
$ vim wallpapers.tar.bz2
  # decompress the .bz2 archive with -d flag
$ bzip2 -d wallpapers.tar.bz2
# add Wood.jpg and Acrylic.jpg back into the archive
$ tar rfv wallpapers.tar -C gnome/ Wood.jpg Acrylic.jpg
# compress the archive again with bzip2
$ bzip2 wallpapers.tar
# list content of the wallpapers.tar.bz2 archive
$ vim wallpapers.tar.bz2
$ tar xzfv wallpapers.tar.gz  # extract files in gzip archive
$ tar tzfv wallpapers.tar.gz # confirm files still exist in archive
$ ls # list working directory
  # remove the files we just extracted
$ rm *.jpg *.png *.xml
# create an empty directory
$ mkdir top
# extract files into the top/ directory and list
$ tar xzfv wallpapers.tar.gz --one-top-level=top
$ ls top
$ rm -fr top
$ tar xzfv wallpapers.tar.gz --one-top-level
$ ls wallpapers
$ tar xfv wallpapers.tar.zst --zstd Wood.jpg        # zstd
$ tar xJfv wallpapers.tar.xz Acrylic.jpg Road.jpg # xz
$ rm *.jpg
$ mkdir example-02
$ mv wallpapers.* wallpapers example-02/
  # create two archives and list their contents
$ tar cfv foo.tar -C gnome/ Acrylic.jpg Road.jpg
$ tar cfv bar.tar -C gnome/ Dark_Ivy.jpg Wood.jpg
$ tar tfv foo.tar && tar tfv bar.tar
# append bar.tar to foo.tar and list the contents again
$ tar Afv foo.tar bar.tar
$ tar tfv foo.tar && tar tfv bar.tar
# compress archive with gzip
$ gzip foo.tar
$ mkdir example-03
$ mv foo.tar.gz bar.tar example-03/

Other Useful tar options

Some other useful tar command line options.
$ man tar

A Closer Look at the Compression Tools

Some useful options for the gzip program on the command line.

Command Line Reference — bzip2

Some useful options for the bzip2 program on the command line.

Command Line Reference — xz

Some useful options for the xz program on the command line.

Command Line Reference — zstd

Some useful options for the zstd program on the command line.

In Conclusion

MSc. Programmer and fan of open source software.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store