> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Extract a tar.gz File on Linux (tar and zip Commands)

> Extract a tar.gz file on Linux with tar -xzf file.tar.gz, or into a folder with -C. Create archives with tar -czf, and use zip and unzip for .zip files.

To extract a `.tar.gz` file on Linux, run `tar -xzf archive.tar.gz`. Add `-C /path/to/folder` to extract somewhere else, and `-v` to list each file as it unpacks. To create one, run `tar -czf backup.tar.gz folder/`. For `.zip` files, use `unzip archive.zip` to extract and `zip -r archive.zip folder/` to create.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Beginner | 5 minutes | Every Linux distribution | `zip`/`unzip` packages for .zip files |

## Before you start

* [Connect with SSH](https://help.freakhosting.com/portals/other/how-to-connect-to-a-linux-server-with-ssh.md).
* `tar` and `gzip` are installed on every Linux server. `zip`, `unzip` and `zstd` may need installing:

> [tab] Ubuntu / Debian
>
> ```bash theme={null}
> sudo apt install zip unzip zstd
> ```

> [tab] AlmaLinux / Rocky
>
> ```bash theme={null}
> sudo dnf install zip unzip zstd
> ```

* Check you have enough free space for the unpacked files with `df -h`.

## How to extract a tar.gz file

**1. Look inside first (optional)**

```bash theme={null}
tar -tzf site-backup.tar.gz
```

```text theme={null}
site/
site/html/
site/html/index.html
site/app.log
```

This shows whether the archive has a top folder, so you know where files will land.

**2. Extract it**

```bash theme={null}
tar -xzf site-backup.tar.gz
```

Files are unpacked into the current folder.

**3. Or extract into another folder**

```bash theme={null}
mkdir -p /home/alex/restore
tar -xzf site-backup.tar.gz -C /home/alex/restore
```

The target folder must exist.

> [!WARNING]
>
> Extracting overwrites files with the same names without asking. Extract into an empty folder first if you aren't sure what the archive contains, and only extract archives from sources you trust.

### What the tar letters mean

| Letter | Meaning |
| - | - |
| `x` | Extract |
| `c` | Create |
| `t` | List contents |
| `z` | gzip (`.tar.gz`, `.tgz`) |
| `J` | xz (`.tar.xz`) |
| `j` | bzip2 (`.tar.bz2`) |
| `v` | Verbose: print each file |
| `f` | The archive file name follows; keep it last |

Modern GNU tar detects the compression when extracting, so `tar -xf file.tar.xz` also works.

## How to extract other archive types

| File | Extract command |
| - | - |
| `.tar.gz` / `.tgz` | `tar -xzf file.tar.gz` |
| `.tar.xz` | `tar -xJf file.tar.xz` |
| `.tar.bz2` | `tar -xjf file.tar.bz2` |
| `.tar.zst` | `tar --zstd -xf file.tar.zst` (needs `zstd`) |
| `.tar` | `tar -xf file.tar` |
| `.gz` (single file) | `gunzip file.gz` (add `-k` to keep the `.gz`) |
| `.zip` | `unzip file.zip` |

### How to extract only one file

```bash theme={null}
tar -xzf site-backup.tar.gz site/html/index.html
```

Use the path exactly as `tar -tzf` lists it.

## How to create a tar.gz archive

```bash theme={null}
tar -czf site-backup.tar.gz site/
```

Useful variations:

```bash theme={null}
# Add today's date to the name
tar -czf "site-$(date +%F).tar.gz" site/

# Archive a folder by absolute path without storing the full path
sudo tar -czf /home/alex/www.tar.gz -C /var/www example.com

# Leave out folders
tar -czf site.tar.gz --exclude="site/cache" --exclude="*.log" site/

# Better compression (slower)
tar -cJf site.tar.xz site/
```

> [!NOTE]
>
> For website or app backups, run `tar` with `sudo` so it can read every file, and stop or pause databases first. Copy database data with a dump tool instead; see [back up and restore MySQL databases](https://help.freakhosting.com/portals/other/how-to-back-up-and-restore-mysql-databases.md).

## How to zip and unzip files

```bash theme={null}
zip -r site.zip site/          # create a zip of a folder
unzip -l site.zip              # list contents
unzip site.zip                 # extract here
unzip site.zip -d /home/alex/restore   # extract into a folder
unzip -o site.zip              # overwrite without asking
```

Use `.zip` when the archive goes to Windows users. Use `.tar.gz` for Linux servers: it keeps file permissions and owners.

## How to check it worked

List the archive and compare the file count with the source:

```bash theme={null}
tar -tzf site-backup.tar.gz | wc -l
find site | wc -l
```

The two numbers match. `gzip -t site-backup.tar.gz` tests the archive for damage and prints nothing when it is fine.

## Frequently asked questions

### What does "gzip: stdin: not in gzip format" mean?

The file isn't really gzip-compressed, often because the download saved an HTML error page. Check with `file archive.tar.gz`. If it says `POSIX tar archive`, extract with `tar -xf` (no `z`).

### How do I extract a .tar.gz file to a specific directory?

Use `-C`: `tar -xzf file.tar.gz -C /path/to/folder`. Create the folder first with `mkdir -p`.

### How do I remove the top folder when extracting?

Add `--strip-components=1`: `tar -xzf wordpress.tar.gz -C /var/www/example.com --strip-components=1` puts the contents of `wordpress/` straight into the target.

### How do I make a password-protected archive?

`zip -e secret.zip file` asks for a password, but zip encryption is weak. For backups, use a tool built for encryption such as [restic](https://help.freakhosting.com/portals/other/how-to-make-encrypted-backups-with-restic.md).

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every server guide in one place.

> [card] [Back up a Linux server](https://help.freakhosting.com/portals/other/how-to-back-up-a-linux-server.md)
>
> Plan proper backups.

> [card] [Copy files with scp and rsync](https://help.freakhosting.com/portals/other/how-to-copy-files-with-scp-and-rsync.md)
>
> Move archives between servers.

> [card] [Check disk space](https://help.freakhosting.com/portals/other/how-to-check-disk-space-on-linux.md)
>
> Make room before extracting.

***

> [!NOTE]
>
> **Checked against the official [GNU tar manual](https://www.gnu.org/software/tar/manual/tar.html) and the Info-ZIP `zip` and `unzip` manuals** on 4 November 2026; commands tested on Ubuntu 24.04 LTS. Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.