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 wantCommand
Extract into the current foldertar -xzf file.tar.gz
Extract and print each file nametar -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 extractingtar -tf file.tar.gz
List with permissions, owner and datestar -tvf file.tar.gz
Extract one file onlytar -xzf file.tar.gz path/inside/archive.txt
Extract files by extensiontar -xzf file.tar.gz --wildcards '*.odt'
Skip the top-level folder inside the archivetar -xzf file.tar.gz --strip-components=1
Create a .tar.gz from a foldertar -czvf archive.tar.gz folder/

What the tar options mean

OptionMeaning
-xExtract files from an archive
-cCreate a new archive
-tList the contents of an archive
-zFilter through gzip (needed when creating a .tar.gz; GNU tar detects it automatically when extracting)
-vVerbose: print each file name as it is processed
-fThe archive file name. It must come right before the file name, which is why the options are written -xzvf file.tar.gz
-C dirChange 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.

Advertisement

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

Terminal showing sudo apt-get install tar reporting tar is already the newest version

To confirm tar works and see its version, run:

tar --version
Terminal showing tar --version output for GNU tar

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.

File manager showing the My_files folder with files.tar.gz, Test.odt, Test.tar.gz and Testfile.txt

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

File manager showing the Data folder and the new Test.tar.gz archive

To extract the archive into the current folder, run:

tar -zxvf Testfile.tar.gz
Terminal showing tar -zxvf Testfile.tar.gz listing the extracted Data folder files

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
Terminal showing tar -xvzf Test.tar.gz listing the extracted Data folder files

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
Terminal showing tar -xvzf Test.tar.gz Data/Testfile.txt extracting a single file

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.

Terminal showing tar -xf files.tar.gz Testfile.txt Test.odt extracting two named files

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

File manager showing the extracted Test.odt and Testfile.txt next to files.tar.gz and Test.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'
Terminal showing tar -xf files.tar.gz Testfile.txt Test.odt (named-file extraction)

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.

Terminal showing tar -xf files.tar.gz Testfile.txt Test.odt (named-file extraction)

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:

File manager showing files.tar.gz, Test.odt, Test.tar.gz and Testfile.txt after extraction

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.

Terminal showing tar -tvf Test.tar.gz listing permissions, owner, size and date for each file

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.

Terminal showing wget piping the blender 2.80 source archive into tar

The blender source folder is extracted into the current directory.

File manager showing the extracted blender-2.80 folder in the home 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

MessageCauseFix
gzip: stdin: not in gzip format / tar: Child returned status 1The 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 directoryWrong file name, wrong folder, or the -C folder does not existCheck with ls and pwd; create the target with mkdir -p
Cannot open: Permission denied / Cannot mkdirYou cannot write to the destination folderExtract into a folder you own, such as your home directory. Use sudo only when you really need system folders
Removing leading `/' from member namesA warning, not an error: tar strips the absolute path when creating an archive from /pathNo action needed; the archive stores relative paths, which is safer to extract
Not found in archiveThe file path you gave does not match the stored pathCopy the exact path from tar -tf archive.tar.gz
Advertisement

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.

Advertisement