How to create and extract tar archives in Linux - Rocketeers app

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com/login) 

 On this page

 Knowledge
---------

How to create and extract tar archives in Linux
===============================================

### [\#CommandLine](https://rocketeersapp.com/knowledge/command-line)

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](https://rocketeersapp.com/author/mark-van-eijk) on June 23, 2026 · 1 minute read

1. [Working with tar archives](#content-working-with-tar-archives)
2. [Create an archive](#content-create-an-archive)
3. [Create a compressed .tar.gz](#content-create-a-compressed-targz)
4. [Extract an archive](#content-extract-an-archive)
5. [Extract to a specific directory](#content-extract-to-a-specific-directory)
6. [List the contents](#content-list-the-contents)
7. [Extract a single file](#content-extract-a-single-file)
8. [Putting it together](#content-putting-it-together)

[\#](#content-working-with-tar-archives "Permalink")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

[\#](#content-create-an-archive "Permalink")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.

[\#](#content-create-a-compressed-targz "Permalink")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.

[\#](#content-extract-an-archive "Permalink")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.

[\#](#content-extract-to-a-specific-directory "Permalink")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

```

[\#](#content-list-the-contents "Permalink")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

```

[\#](#content-extract-a-single-file "Permalink")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.

[\#](#content-putting-it-together "Permalink")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!

  Fill in your email address to receive updates  Subscribe 

#### More in [\#CommandLine](https://rocketeersapp.com/knowledge/command-line)

- [Argument list too long (Bash: /bin/rm)](https://rocketeersapp.com/knowledge/argument-list-too-long)
- [How to install Composer packages locally](https://rocketeersapp.com/knowledge/install-composer-packages-locally)
- [How to send GET and POST requests with curl](https://rocketeersapp.com/knowledge/curl-post-get-api-requests)
- [Essential Linux command line basics for developers](https://rocketeersapp.com/knowledge/linux-command-line-basics)
- [How to search file contents with grep](https://rocketeersapp.com/knowledge/search-files-grep-command)
- [The complete guide to the curl command](https://rocketeersapp.com/knowledge/curl-command-complete-guide)

 [View all 21 articles →](https://rocketeersapp.com/knowledge/command-line)
