Knowledge

How to create and extract tar archives in Linux

#CommandLine

Create, compress, list and extract tar archives in Linux — including .tar.gz files, extracting to a directory and pulling out a single file from an archive.

Published by Mark van Eijk on June 23, 2026 · 1 minute read

  1. Working with tar archives
  2. Create an archive
  3. Create a compressed .tar.gz
  4. Extract an archive
  5. Extract to a specific directory
  6. List the contents
  7. Extract a single file
  8. Putting it together

Working with tar archives

tar bundles many files and directories into a single archive file, optionally compressed. I use it constantly for backups, releases and moving directories between servers. The flags look cryptic at first, but they're just letters you combine.

The ones you'll use most:

  • c — create a new archive
  • x — extract an archive
  • t — list the contents
  • v — verbose, print each file as it's processed
  • f — the next argument is the archive filename (always needed)
  • z — pass the archive through gzip compression

Create an archive

Bundle a directory into a .tar file with c, v and f:

tar -cvf backup.tar ./project

This creates backup.tar containing the project directory, printing each file as it goes.

Create a compressed .tar.gz

Add z to gzip the archive as it's built. The convention is to name it .tar.gz (or .tgz):

tar -czvf backup.tar.gz ./project

Compressed archives are much smaller and the standard choice for backups or transferring over the network.

Extract an archive

Swap c for x to extract. For an uncompressed archive:

tar -xvf backup.tar

For a gzip-compressed one, add z again:

tar -xzvf backup.tar.gz

Modern versions of tar actually detect the compression automatically, so tar -xvf backup.tar.gz usually works too — but being explicit with z never hurts.

Extract to a specific directory

By default files land in the current directory. Use -C to extract somewhere else (the target directory must already exist):

tar -xzvf backup.tar.gz -C /var/www/releases

List the contents

Before extracting an archive someone handed you, it's worth peeking inside with t. This lists every entry without unpacking anything:

tar -tvf backup.tar.gz

Extract a single file

You don't have to unpack the whole thing. Name the exact path (as shown by the listing above) after the archive to pull out just that file:

tar -xzvf backup.tar.gz project/config/app.php

This recreates only project/config/app.php relative to your current directory.

Putting it together

Once the letters click, tar reads naturally: -czvf is "create, gzip, verbose, file" and -xzvf is "extract, gzip, verbose, file". Those two cover the large majority of what you'll ever need.

Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

Related articles

Argument list too long (Bash: /bin/rm)

Create, compress, list and extract tar archives in Linux — including .tar.gz files, extracting to a directory and pulling out a single file from an archive.

Read more →

How to install Composer packages locally

Create, compress, list and extract tar archives in Linux — including .tar.gz files, extracting to a directory and pulling out a single file from an archive.

Read more →