# Linux commands: tar

> Learn how the Linux tar command groups files into an archive with tar -cf, extracts them with tar -xf, and makes a gzip-compressed .tar.gz with the z flag.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-10-13 | Topics: [CLI](https://flaviocopes.com/tags/cli/) | Canonical: https://flaviocopes.com/linux-command-tar/

The `tar` command is used to create an archive, grouping multiple files in a single file.

Its name comes from the past and means _tape archive_. Back when archives were stored on tapes.

This command creates an archive named `archive.tar` with the content of `file1` and `file2`:

```bash
tar -cf archive.tar file1 file2
```

> The `c` option stands for _create_. The `f` option is used to write to file the archive.

To extract files from an archive in the current folder, use:

```bash
tar -xf archive.tar
```

> the `x` option stands for _extract_

and to extract them to a specific directory, use:

```bash
tar -xf archive.tar -C directory
```

You can also just list the files contained in an archive:

![Terminal showing tar -tf archive.tar command listing file1 and file2 contents of the archive](https://flaviocopes.com/images/linux-command-tar/Screen_Shot_2020-09-09_at_16.56.33.png)

`tar` is often used to create a **compressed archive**, gzipping the archive.

This is done using the `z` option:

```bash
tar -czf archive.tar.gz file1 file2
```

This is just like creating a tar archive, and then running `gzip` on it.

To unarchive a gzipped archive, you can use `gunzip`, or `gzip -d`, and then unarchive it, but `tar -xf` will recognize it's a gzipped archive, and do it for you:

```bash
tar -xf archive.tar.gz
```

The `tar` command works on Linux, macOS, WSL, and anywhere you have a UNIX environment

If you can never remember the right flags, I made a [tar command builder](https://flaviocopes.com/tools/cli-builder/) that composes the command for you.
