Skip to content

Linux basicsFor beginnersDuration around 5 min.

How to unzip and extract archives in Linux: tar, gzip and unzip

Software, backups and data usually arrive on a server as .tar.gz or .zip. This guide shows the commands for packing, viewing and extracting, each with an example to copy.

Tested on with GNU tar 1.35 and UnZip 6.0 on Ubuntu 24.04 LTS and Ubuntu 26.04 LTS

At a glance

  • tar -czf packs, tar -tzf shows the contents, tar -xzf extracts — with -C into a different folder.
  • When extracting, tar -xf detects the compression automatically, whether gzip, xz or zstd.
  • For .zip files you need unzip; with -d you select the target folder.

Prerequisites

  • A Linux system — such as a VPS or a Cloud Server. tar and gzip are always installed on Ubuntu, unzip and zip are not always.

The examples pack the folder projekt with one subfolder level. The letters in tar stand for: c create, x extract, t list, z gzip, f filename follows.

1. Pack folder as .tar.gz

Create archive
tar -czf projekt.tar.gz projekt

You choose the filename after -f yourself; the .tar.gz extension shows others how to open the file.

2. View contents without extracting

List contents
tar -tzf projekt.tar.gz
Output
projekt/
projekt/x.log
projekt/a.txt
projekt/sub/
projekt/sub/b.txt

3. Extract, including to a different folder

Without further specification, tar extracts into the current folder. With -C you select the target; the folder must already exist.

Extract
tar -xzf projekt.tar.gz
mkdir -p /tmp/ziel && tar -xzf projekt.tar.gz -C /tmp/ziel

You do not need to specify the compression when extracting — tar -xf detects it from the contents:

Automatic compression
tar -xf projekt.tar.xz
tar -xf projekt.tar.zst

4. Other compression, exclude files, single file

xz packs smaller but takes longer; zstd is fast and still packs well. tar recognises both formats automatically.

Pack with xz or zstd
tar -cJf projekt.tar.xz projekt
tar --zstd -cf projekt.tar.zst projekt

With --exclude, files matching the pattern are left out:

Exclude logs
tar -czf projekt.tar.gz --exclude='*.log' projekt

You can extract a single file using its path in the archive:

Extract only one file
tar -xzf projekt.tar.gz projekt/sub/b.txt

5. Files in .zip format: unzip and zip

Install unzip and zip packages
sudo apt update
sudo apt install -y unzip zip

unzip -l shows the contents, -d selects the target folder and creates it if necessary. With a pattern in quotation marks, you only extract a part.

View and extract
unzip -l projekt.zip
unzip projekt.zip -d entpackt
unzip projekt.zip 'projekt/sub/*' -d teil
Pack folder as .zip
zip -r projekt.zip projekt

Permissions and owners

tar preserves permissions and, if you extract as root, also the owners. For server backups, .tar.gz is therefore a better choice than .zip.

6. Single file with gzip

gzip packs exactly one file and replaces it with the packed version; -k keeps the original.

Pack and extract file
gzip -k bericht.txt
gunzip bericht.txt.gz

Typical errors

  • Wrong format: The file is not a gzip archive, often a .zip file with the wrong extension. Check with file projekt.tar.gz what it really is.
A .zip file with tar -xzf
gzip: stdin has more than one entry--rest ignored
tar: Child returned status 2
  • Cannot open: Name or path is incorrect. If the archive is in a different folder, include the path.
Archive not found
tar (child): projekt.tar.gz: Cannot open: No such file or directory
  • unzip: command not found: Install unzip as in step 5.

We tested all commands in this guide on 2 December 2026 — with GNU tar 1.35 and UnZip 6.0 on Ubuntu 24.04 LTS and Ubuntu 26.04 LTS. If a step changes at the manufacturer, we update it here along with the date.

Official documentation

Frequently asked questions about tar and unzip

What is the difference between .tar and .tar.gz?

A .tar archive only bundles files together. With .tar.gz, this archive is additionally compressed with gzip and thus usually much smaller.

How do I extract to a specific folder?

For tar, use -C and the folder, which must already exist. For unzip, use -d; the folder will be created if necessary.

Does extracting overwrite existing files?

tar overwrites without prompting. unzip prompts you, unless you use -o to overwrite or -n to keep existing files.

Which format should I use for a backup?

.tar.gz or .tar.zst: both preserve permissions and ownership. zstd is faster, xz compresses the smallest but takes the longest.

How do I see which files tar is currently processing?

With -v, tar outputs every file it compresses or extracts, for example with tar -xvzf projekt.tar.gz.

Further reading

More tutorials

  • The scp command with examples: copy files over SSH

    Linux basicsFor beginnersabout 5 min

    scp copies files over an SSH connection — encrypted, with the same credentials as your login, and without an additional service on the server. This guide shows the commands for common use cases, each with an example to copy.

  • How to check your Ubuntu version, codename and kernel

    Linux basicsFor beginnersabout 2 min

    A single command in the terminal shows which Ubuntu version is running on a server. Here are the common ways with their output — for the version with point release, the codename, the kernel and the architecture.

  • How to remove a directory in Linux with rm and rmdir

    Linux basicsFor beginnersabout 5 min

    In Linux, you delete an empty folder with rmdir and a folder including its contents with rm -r. This tutorial shows both commands with their messages, the variants with and without prompting, and what to check before running rm -rf — because there is no recycle bin in the terminal.

A server for your archives

Every VPS comes with tar and gzip; unzip is installed with one line.