Last updated:
Quick answer: open a terminal in the folder that contains the archive and run tar -xzvf file.tar.gz. That extracts everything into the current directory. To extract into another folder, add -C /path/to/folder. These commands were run on Ubuntu 24.04 with GNU tar 1.35, the tar that Ubuntu and Debian install by default.
tar -xzvf file.tar.gz
A .tar.gz (or .tgz) file is a tar archive, which bundles many files and folders into one, that has been compressed with gzip. Linux does both steps with one command, tar. The table below covers the commands people look for most often.
| What you want | Command |
|---|---|
| Extract into the current folder | tar -xzf file.tar.gz |
| Extract and print each file name | tar -xzvf file.tar.gz |
| Extract into a specific folder (it must already exist) | tar -xzf file.tar.gz -C /path/to/folder |
| List the contents without extracting | tar -tf file.tar.gz |
| List with permissions, owner and dates | tar -tvf file.tar.gz |
| Extract one file only | tar -xzf file.tar.gz path/inside/archive.txt |
| Extract files by extension | tar -xzf file.tar.gz --wildcards '*.odt' |
| Skip the top-level folder inside the archive | tar -xzf file.tar.gz --strip-components=1 |
| Create a .tar.gz from a folder | tar -czvf archive.tar.gz folder/ |
What the tar options mean
| Option | Meaning |
|---|---|
-x | Extract files from an archive |
-c | Create a new archive |
-t | List the contents of an archive |
-z | Filter through gzip (needed when creating a .tar.gz; GNU tar detects it automatically when extracting) |
-v | Verbose: print each file name as it is processed |
-f | The archive file name. It must come right before the file name, which is why the options are written -xzvf file.tar.gz |
-C dir | Change to this directory before extracting |
On GNU tar, tar -xf file.tar.gz also works because tar detects gzip, bzip2 (.tar.bz2) and xz (.tar.xz) when reading. We extracted a .tar.gz, a .tar.xz and a .tar.bz2 with the same tar -xf command on Ubuntu 24.04 to confirm this.
Check that tar is installed
tar is part of the default install on Ubuntu and Debian, so you normally do not need to install it. If tar --version says the command is not found (for example in a minimal container image), install it with:
sudo apt-get install tar
sudo runs the command as root and apt-get install tar installs the package (sudo apt install tar works the same way).

To confirm tar works and see its version, run:
tar --version

To practise extracting, you first need an archive. This command creates one from files or a folder (see also how to compress a folder with tar for more options):
tar -czvf project.tar.gz file1 folder1
-c creates a new archive, -z compresses it with gzip, -v prints each file as it is added and -f project.tar.gz sets the archive name. Archives you create this way contain the paths you passed on the command line, so run the command from the parent folder and use relative paths.

The archive appears in the folder after the command finishes (here it is named Test.tar.gz):

To extract the archive into the current folder, run:
tar -zxvf Testfile.tar.gz

The order of the letters does not matter as long as f is last, so this is the same command:
tar -xvzf Testfile.tar.gz

Extract into a different folder with -C
By default tar writes into the current directory. Use -C to choose the destination. The folder must already exist, otherwise tar stops with Cannot open: No such file or directory.
mkdir -p ~/extracted
tar -xzvf project.tar.gz -C ~/extracted
If the archive contains a single top-level folder you do not want, --strip-components=1 removes that first path level. With an archive that holds p/d/a.txt, this command produced d/a.txt in the target folder:
tar -xzvf archive.tar.gz -C ~/extracted --strip-components=1
Extract a single file or folder
Give the exact path as shown by tar -tf. This extracts only that file and recreates its folders:
tar -xvzf Test.tar.gz Data/Testfile.txt

You can name several files in one command:
tar -xf filename.tar.gz file1 file2
-xf tells tar to extract the files you named after the archive name.
Stuck at this step? Ask us, it is free and include the exact error.

Only the two named files, Test.odt and Testfile.txt, are extracted from files.tar.gz.

To extract by pattern, such as every .js, .mp4 or .odt file, use --wildcards and quote the pattern so your shell does not expand it first:
tar -xf filename.tar.gz --wildcards '*.odt'

Only the matching files are extracted. The screenshots below show the earlier named-file command; our own --wildcards '*.odt' test on Ubuntu 24.04 extracted only the .odt file.

To see what is inside an archive without extracting it, use -t. It is a good habit before extracting an archive you downloaded:
tar -tf Test.tar.gz
-tf prints one path per line:

Add -v to also see permissions, owner, size and modification date:
tar -tvf filename.tar.gz
Example listing format (from our test archive): -rw-r--r-- root/root 2 2026-10-30 08:15 project/c.js.

You can also download and extract in one step by piping wget into tar, without saving the archive. The screenshot below shows an older run that used sudo; you should not use sudo here, because it extracts downloaded files as root. Only do this with sources you trust, and preferably list the archive first.
wget -qO- https://download.blender.org/source/blender-2.80.tar.gz | tar -xz
wget -qO- writes the download to standard output and tar -xz reads the archive from standard input.

The blender source folder is extracted into the current directory.

Verify the extraction
List the target folder and compare it with the archive listing. If tar printed nothing and the exit status is 0, the extraction worked:
tar -xzf project.tar.gz -C ~/extracted; echo "exit status: $?"
ls -R ~/extracted | head
Common errors and fixes
| Message | Cause | Fix |
|---|---|---|
gzip: stdin: not in gzip format / tar: Child returned status 1 | The file is not gzip data (often an HTML error page saved as .tar.gz, or a plain .tar renamed) | Run file archive.tar.gz, then re-download. With GNU tar tar -xf picks the right decompressor for real gzip, bzip2 and xz files |
Cannot open: No such file or directory | Wrong file name, wrong folder, or the -C folder does not exist | Check with ls and pwd; create the target with mkdir -p |
Cannot open: Permission denied / Cannot mkdir | You cannot write to the destination folder | Extract into a folder you own, such as your home directory. Use sudo only when you really need system folders |
Removing leading `/' from member names | A warning, not an error: tar strips the absolute path when creating an archive from /path | No action needed; the archive stores relative paths, which is safer to extract |
Not found in archive | The file path you gave does not match the stored path | Copy the exact path from tar -tf archive.tar.gz |
Frequently asked questions
How do I extract a .tar.gz file to a specific directory?
Use tar -xzf file.tar.gz -C /target/folder. The target folder must already exist.
Is .tgz the same as .tar.gz?
Yes. .tgz is a shorter name for a gzip-compressed tar archive, and the same tar -xzf command extracts it.