Extract a Compressed tar Archive

Extract a Compressed tar Archive

The school receives a compressed tar package of term reports. Extracting it reconstructs the member paths as files and directories, so your destination choice matters. An archive can contain many entries, and familiar-looking names can collide with work already saved in a folder.

We will inspect our own small archive, then extract into a separate empty destination. This gives you a predictable exercise and a sensible inspection habit. Listing is useful, but it is not a complete safety check for an unfamiliar archive's contents, paths or links.

What you’ll learn

  • List and extract a compressed tar archive.
  • Choose an empty destination and consider names and overwrites before extraction.

Extraction creates entries from archive members

  1. Inspect

    tar -tzf lists names in the compressed archive.

    term/students.txt
  2. Destination

    An empty restored directory separates extracted files from current work.

    restored/
  3. Extract

    tar -xzf with -C places the archived tree under that directory.

    restored/term/students.txt
Keep listing, extraction and destination selection separate in your reasoning even when their commands are close together.

Inspect a term archive before extracting it

Prepare the example

Start in a new directory with tar and gzip support available. This block prepares its own term folder and creates the input archive before extraction. The source is known exercise data. We also create restored as a separate destination instead of extracting over the original report tree.

Bash · Linux terminal

mkdir term
printf 'Diya\n' > term/students.txt
tar -czf term.tar.gz term
mkdir restored
tar -tzf term.tar.gz
tar -xzf term.tar.gz -C restored

Expected behavior; machine-dependent details

Archive entries are listed, then files appear under restored.

The first lines create a student list and package it using tar -czf: create, gzip-compress, and name the archive. After mkdir restored, tar -tzf term.tar.gz lists the compressed archive's members. Nothing has been extracted by that listing operation.

Finally, tar -xzf term.tar.gz -C restored extracts the gzip-compressed package after changing the extraction destination to restored. The member term/students.txt therefore appears beneath that directory. Read the restored file to check its content. For an unfamiliar package, assess its origin and member paths and use an appropriate isolated workflow rather than assuming an empty folder alone removes every risk.

Choose extraction options for the actual format

Tar archives can be uncompressed or combined with compression such as gzip or bzip2. The extraction command changes slightly depending on the archive format.

The extraction flags depend on how the archive was compressed. The common formats in this lesson are:

  • .tar – just an archive, no compression
  • .tar.gz or .tgz – archive + gzip compression
  • .tar.bz2 – archive + bzip2 compression

1. Extracting a .tar file

Imagine you’ve downloaded a file called project.tar. This file is just a collection of files bundled together without compression.

To extract it, run:

tar -xvf project.tar

Let’s break it down:

  • -x means extract
  • -v stands for verbose (shows progress)
  • -f specifies the file name to work with

Here's what the output might look like:

file1.txt
file2.txt
images/
images/logo.png

2. Extracting a .tar.gz or .tgz file

These files are archived and compressed using gzip. The command is just as simple:

tar -xvzf project.tar.gz

Or if the file is named project.tgz:

tar -xvzf project.tgz

Here’s the output:

project/
project/main.cpp
project/README.md

And again, here’s what the options mean:

  • -z tells tar to use gzip
  • -xvzf is the usual combo to extract and see progress

3. Extracting a .tar.bz2 file

If your file uses bzip2 compression, like project.tar.bz2, you’ll need to use the -j flag:

tar -xvjf project.tar.bz2

That tells tar to use bzip2 while extracting.

project/
project/index.html
project/style.css

Optional: Extract to a Specific Directory

Want to extract files to a custom folder? Use the -C option:

tar -xvzf project.tar.gz -C /home/yourname/projects/

Match tar extraction flags to the archive format

Here’s a quick cheat sheet:

  • tar -xvf file.tar – extract .tar
  • tar -xvzf file.tar.gz – extract .tar.gz or .tgz
  • tar -xvjf file.tar.bz2 – extract .tar.bz2

And that’s it! You've now mastered the basics of extracting compressed files using tar in Linux.

If this was helpful, be sure to check out our other beginner-friendly Linux guides.

Try it, then check the worked answer

Your task: Choose an empty extraction directory.

Start in a new directory with tar and gzip support available. This block prepares its own term folder and creates the input archive before extraction. The source is known exercise data. We also create restored as a separate destination instead of extracting over the original report tree.

Bash · complete example for this question

mkdir term
printf 'Diya\n' > term/students.txt
tar -czf term.tar.gz term
mkdir restored
tar -tzf term.tar.gz
tar -xzf term.tar.gz -C restored

Expected behavior; machine-dependent details

Archive entries are listed, then files appear under restored.

Worked answer

tar -xzf term.tar.gz -C restored

Select the extraction form with -C restored. The operation creates entries from members; it does not only change the archive itself. The list form, using -t, helps you inspect names but does not perform extraction.

Check your decision

Choose an empty extraction directory.
Choose one answer. You can try again.

Answer and explanation

tar -xzf term.tar.gz -C restored

Select the extraction form with -C restored. The operation creates entries from members; it does not only change the archive itself. The list form, using -t, helps you inspect names but does not perform extraction.

Inspect members and choose the extraction directory

Keep listing, extraction and destination selection separate in your reasoning even when their commands are close together. Inspect member names and avoid overwriting working reports. Validate the restored paths and contents, and apply more isolation when the package is not your own known exercise archive.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Extract a ZIP Archive →