The basic command to untar a file

To extract a tar file in Linux, use the tar command with the -x flag, which stands for "extract". The most common form is:

tar -xf filename.tar

This command unpacks the contents of filename.tar into your current directory. The -f flag tells tar which file to work with. You do not need to be root or use sudo for this — you just need read permission on the tar file itself.

If the tar file is compressed (which most are), you add a flag for the compression type. A .tar.gz file uses gzip compression, so extract it with:

tar -xzf filename.tar.gz

The -z flag handles the gzip decompression automatically. You do not need to decompress it separately first.

Key Takeaways

  • Use tar -xf filename.tar to extract an uncompressed tar file into your current directory.
  • Use tar -xzf filename.tar.gz for gzip-compressed files, which is the most common format you will encounter.
  • Use tar -xjf filename.tar.bz2 for bzip2-compressed files, which compress more tightly but take longer to extract.
  • Add the -v flag (verbose) to see each file being extracted, which helps you verify the contents are what you expected.
  • Use tar -xf filename.tar -C /path/to/directory to extract into a specific folder instead of your current location.

Extracting to a specific directory

By default, tar extracts everything into your current working directory. If you want the contents to go somewhere else, use the -C flag followed by the path:

tar -xf filename.tar -C /home/username/Documents

This extracts the tar file's contents into /home/username/Documents instead. The directory must already exist — tar will not create it for you. If you are extracting a large archive and want to make sure it goes to a location with enough disk space, check first with df -h to see how much free space each partition has.

The -C flag is useful when you download a tar file to your Downloads folder but want to unpack it into your home directory or a project folder. It saves you from extracting in one place and then moving files around afterward.

Handling different compression formats

Not every tar file uses gzip compression. You may encounter three main formats:

.tar.gz (or .tgz) uses gzip compression. Extract with tar -xzf. This is the most common format and compresses moderately well.

.tar.bz2 uses bzip2 compression, which is tighter but slower. Extract with tar -xjf filename.tar.bz2. The -j flag handles bzip2 decompression.

.tar with no compression suffix is uncompressed. Extract with tar -xf filename.tar using only the -x and -f flags. These are less common now because compression saves disk space and download time.

If you are unsure which format you have, the filename usually tells you. If you try the wrong flag, tar will give you an error message like "gzip: stdin: not in gzip format" — that tells you to try a different compression flag instead.

Viewing contents before extracting

Before you extract a tar file, you may want to see what is inside it without unpacking everything. Use the -t flag (for "list") instead of -x:

tar -tzf filename.tar.gz

This shows you every file and folder in the archive without extracting anything. It is useful when you download a tar file from the internet and want to verify it contains what you expect before you unpack it. You can also pipe this to grep to search for a specific file:

tar -tzf filename.tar.gz | grep searchterm

This tells you whether a particular file is in the archive. If the archive is very large, listing the contents first can save you from extracting gigabytes of data you do not actually need.

Using verbose mode to see what is happening

When you extract a large tar file, you may wonder if the command is actually working or if it is stuck. Add the -v flag (for "verbose") to see each file as it is extracted:

tar -xzvf filename.tar.gz

Now tar prints the name of each file to the screen as it unpacks it. This is slower than running without -v because printing to the screen takes time, but it gives you confidence the extraction is progressing. For very large archives, you may see hundreds or thousands of filenames scroll past.

Verbose mode is also helpful when something goes wrong. If extraction stops with an error, the last filename printed tells you where the problem occurred. This helps you decide whether to try again or investigate the tar file itself.

Troubleshooting common extraction problems

If you get a "permission denied" error, you do not have read permission on the tar file. Check the file permissions with ls -l filename.tar. If you own the file, add read permission with chmod +r filename.tar, then try extracting again. If someone else owns it, ask them to make it readable or copy it to a location where you have write permission.

If you get "no such file or directory" errors during extraction, the tar file may be corrupted or incomplete. Try listing the contents first with tar -tzf to see if tar can read the file at all. If listing fails, the download may have been interrupted — try downloading the file again.

If extraction creates a single folder with all the contents inside (rather than unpacking files directly into your current directory), that is normal behavior — the person who created the tar file packaged it that way. You can move the contents out afterward with mv if you prefer, or just navigate into the folder.

Extracting only specific files from a tar archive

If a tar file contains many files but you only need one or two, you can extract just those files instead of unpacking everything. List the archive first to find the exact filename:

tar -tzf filename.tar.gz | grep filename

Once you know the exact path inside the archive, extract only that file:

tar -xzf filename.tar.gz path/to/specific/file

This is much faster than extracting a gigabyte archive when you only need a single document. You can also extract multiple specific files by listing them one after another at the end of the command, separated by spaces.

Frequently Asked Questions

Do I need to decompress a .tar.gz file before I can untar it?

No. The tar -xzf command handles both decompression and extraction in one step. You do not need to run gunzip first. Running both would be redundant and would create an intermediate .tar file you do not need.

What is the difference between tar -xf and tar xf?

They do the same thing. Older Unix syntax allowed you to omit the dash before single-letter flags, so tar xf filename.tar works the same as tar -xf filename.tar. Modern practice includes the dash, but both forms are accepted.

Can I extract a tar file on Windows?

Windows 10 and later include built-in tar support in PowerShell, so you can use the same commands. On older Windows versions, you need a third-party tool like 7-Zip or WinRAR. If you are using Windows Subsystem for Linux (WSL), you have full Linux tar support available.

Why does my extracted folder have a different name than the tar file?

The folder name comes from what was inside the tar file when it was created, not from the filename itself. If you downloaded project-1.2.tar.gz but it extracts to a folder called project, that is what the creator named it. You can rename the folder afterward with mv if you prefer.

How do I know if a tar file is corrupted?

Try listing its contents with tar -tzf filename.tar.gz. If tar can read the file structure, it is probably not corrupted. If you get errors like "unexpected end of file" or "gzip: stdin: not in gzip format", the download may have been interrupted or the file is damaged. Try downloading it again.