Skip to main content
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.

Before you start

  • Connect with SSH.
  • tar and gzip are installed on every Linux server. zip, unzip and zstd may need installing:
  • 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)

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

Extract it

Files are unpacked into the current folder.
3

Or extract into another folder

The target folder must exist.
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

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

How to extract other archive types

How to extract only one file

Use the path exactly as tar -tzf lists it.

How to create a tar.gz archive

Useful variations:
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.

How to zip and unzip files

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:
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.

VPS and dedicated server guides

Every server guide in one place.

Back up a Linux server

Plan proper backups.

Copy files with scp and rsync

Move archives between servers.

Check disk space

Make room before extracting.

Checked against the official GNU tar manual 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.

Need Extra Help?

If you encounter any issues, our support team is ready to assist:

Save on Your Hosting

Ready to upgrade your hosting? Use code KB20 at checkout for 20% off your first order!

Last Updated: November 2026