How to Use the tar Command to Compress and Extract Archives in Linux

When transferring multiple files across the internet or backing up a server, you rarely want to handle thousands of individual documents. Instead, you group them together into a single, compressed archive. In the Windows ecosystem, this is usually a `.zip` file. In the Linux world, the undisputed standard is the tarball (`.tar.gz`).

The tar (Tape Archive) command is one of the oldest and most essential utilities in Unix-like operating systems. In this guide, you will learn the essential terminal commands to create, view, and extract tar archives efficiently.

Understanding the ‘tar’ Syntax

Unlike many modern Linux commands, tar does not strictly require a hyphen (-) before its options, though it is good practice to use one. The command structure looks like this:

tar [options] [archive_name.tar.gz] [files_to_compress]

You only need to memorise four primary flags to perform almost any operation:

  • -c : Create a new archive.
  • -x : eXtract an existing archive.
  • -z : Compress the archive using gzip (makes the file size much smaller).
  • -f : Specifies the Filename of the archive (this must always be the last flag before the filename).
  • -v : Verbose mode (prints the names of the files being processed to the screen).

1. How to Compress Files (Create an Archive)

Suppose you have a directory named /var/www/website containing thousands of HTML files and images. You want to back this up into a single, compressed file named backup.tar.gz.

Run the following command:

tar -czvf backup.tar.gz /var/www/website

Let’s break down exactly what happened:

  • c told tar to create an archive.
  • z told tar to compress it using gzip.
  • v told tar to show you what it is doing on screen.
  • f specified that the very next word (backup.tar.gz) is the name of the file to create.
  • Finally, /var/www/website is the target folder you wanted to compress.

2. How to Extract Files (Unzip an Archive)

If you download a software package named software_v2.tar.gz and need to extract its contents into your current directory, use the extract (-x) flag instead of the create flag.

tar -xzvf software_v2.tar.gz

The archive will decompress, and a new folder containing the files will appear in your current directory.

Extracting to a Specific Directory

By default, tar extracts files exactly where you are standing. If you want to extract a downloaded archive directly into the /opt/ folder, use the -C (Change directory) flag at the end of the command.

sudo tar -xzvf software_v2.tar.gz -C /opt/

3. Viewing the Contents of an Archive Without Extracting

Sometimes you download an archive but want to verify its contents before extracting thousands of files onto your hard drive. You can list (-t) the contents without extracting them.

tar -tzf archive.tar.gz

This will print a list of every file inside the archive to the terminal screen.

4. Working with Different Compression Types

While .tar.gz (gzip) is the most common format because it is fast, you will occasionally encounter .tar.bz2 or .tar.xz files. These formats offer much tighter compression (smaller file sizes) but take longer to process.

  • To extract a .tar.bz2 file, replace the -z flag with -j:
    tar -xjvf archive.tar.bz2
  • To extract a .tar.xz file, replace the -z flag with -J:
    tar -xJvf archive.tar.xz

By mastering these four basic combinations (czvf, xzvf, xzvf -C, and tzf), you can confidently handle file backups and software installations across any Linux distribution.

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.