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

When you download source code or backup archives on a Linux server, you will rarely encounter standard .zip files. Instead, you will almost exclusively encounter files ending in .tar.gz. These are colloquially known as “tarballs.” To bundle hundreds of files together, compress them to save space, and extract them later, you must use the foundational Linux tar command.

The tar (Tape Archive) command was originally designed decades ago to write data sequentially onto magnetic tape drives. Today, it remains the standard method for archiving directories. In this guide, you will learn the exact flags required to create and extract these critical files.

Understanding the Flags

Unlike modern graphical zip tools, tar requires you to pass specific flags (letters) to tell the command exactly what to do. The syntax is always tar [flags] [archive_name] [target_files].

Here are the five essential flags you must memorize:

  • -c (Create): Create a brand new archive.
  • -x (Extract): Unpack an existing archive.
  • -v (Verbose): Show a scrolling list of the files being processed so you know the command hasn’t frozen.
  • -f (File): Specify the name of the archive file you are creating or extracting. (This must always be the last flag before the filename).
  • -z (Gzip): Compress the archive using gzip to drastically reduce the file size.

How to Compress a Directory (Create a Tarball)

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

Open your terminal and type:

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

Let’s break down exactly what you just commanded:

  1. -c: Create a new archive.
  2. -z: Compress it using gzip to save space.
  3. -v: Print every file name to the screen as it is added.
  4. -f backup.tar.gz: Name the resulting file backup.tar.gz.
  5. /var/www/website: The source directory you want to compress.

How to Extract a Tarball

When you download a software package, such as wordpress.tar.gz, you need to extract its contents into your current directory.

Type the following command:

tar -xzvf wordpress.tar.gz

The breakdown is almost identical to creation, except you replaced the -c (create) flag with the -x (extract) flag.

Extracting to a Specific Directory

By default, tar extracts files into the folder you are currently sitting in. If you want to unpack an archive into a completely different location, you must use the uppercase -C flag (Change directory).

tar -xzvf archive.tar.gz -C /opt/software/

This command safely unzips the contents directly into the /opt/software/ directory without forcing you to move the files manually afterward.

How to View the Contents Without Extracting

If you find a random mystery.tar.gz file on a server and you want to see what is inside it before dumping its contents into your filesystem, you can use the -t (list) flag.

tar -tzvf mystery.tar.gz

This prints a complete list of every file stored inside the archive, allowing you to safely inspect it without extracting a single byte.

By mastering the tar command and its standard flags, you can confidently navigate the Linux ecosystem, efficiently backing up critical configurations and installing complex software packages directly from the terminal.

Related posts

  1. How to Clear the Terminal Screen in Linux Using the clear Command
  2. How to Download Files Using the curl Command in Linux
  3. How to Clear the Bash History in Linux

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.