Extract a ZIP Archive

Extract a ZIP Archive

A colleague sends a ZIP of term reports, and the teacher wants to unpack it without mixing it into the current working folder. unzip can both list the package and extract its members. Those are different operations, even though they use the same utility.

Let's use a package created by our own example so the contents are known. We will choose a destination explicitly and follow a member path into that destination, rather than guessing where the reports appeared.

What you’ll learn

  • Inspect ZIP members and extract them to a chosen directory.
  • Distinguish listing an archive from extracting its files.

Inspect first, then choose where members go

  1. List

    unzip -l shows member names without creating the files.

    term/students.txt
  2. Choose

    The -d option selects the extraction directory.

    restored/
  3. Extract

    unzip creates member paths beneath that destination, subject to names and overwrite choices.

    restored/term/students.txt
Read a ZIP member list first, then decide where extraction should write.

Extract the term ZIP into restored

Prepare the example

Use a fresh folder with both zip and unzip installed. The block creates a term folder, saves its class list and builds the input ZIP. No earlier lesson's archive is required. Keep the destination separate from the original tree.

Bash · Linux terminal

mkdir term
printf 'Diya\n' > term/students.txt
zip -r term.zip term
unzip -l term.zip
unzip term.zip -d restored

Expected behavior; machine-dependent details

Listing and extraction messages vary with archive contents.

After creating the package, unzip -l term.zip lists its members. It helps you see the directory paths stored inside without writing those entries out. Then unzip term.zip -d restored chooses restored as the extraction destination.

The archive member term/students.txt becomes restored/term/students.txt; the stored directory structure still matters. If files at the destination already exist, handle overwrite questions deliberately. A chosen destination does not turn an arbitrary archive into trusted content, so use additional care and appropriate isolation for unfamiliar input.

Read the member list and handle destination files

The unzip command reads ZIP archives and writes their files back to disk. You can extract into the current directory, choose another directory, or inspect an archive without unpacking it.

Step 1: Make Sure unzip is Installed

Most Linux distributions come with unzip pre-installed. But if yours doesn't, you can install it easily using your package manager.

sudo apt install unzip   # For Ubuntu/Debian
sudo yum install unzip   # For CentOS/RHEL
sudo dnf install unzip   # For Fedora

Once installed, you're good to go.

Step 2: Extract a ZIP File

Now let’s say you have a ZIP file called project.zip. To extract it, just run:

unzip project.zip

This will extract the contents of project.zip into the current directory.

Expected output for this example:

Archive:  project.zip
  inflating: index.html
  inflating: style.css
  inflating: script.js

Step 3: Extract to a Specific Directory

If you want to extract the ZIP file into a folder named project/, you can do this:

unzip project.zip -d project/

This is especially useful for keeping things clean and organized.

Step 4: View the Contents Without Extracting

Want to peek inside the ZIP without extracting it? You can list the contents using:

unzip -l project.zip

Expected output for this example:

Archive:  project.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     1200  2025-07-02 09:00   index.html
     9000  2025-07-02 09:00   style.css
     3400  2025-07-02 09:00   script.js
---------                     -------
    13600                     3 files

Step 5: Overwrite or Skip Existing Files

If some files already exist in the directory, unzip will ask you whether to overwrite them. You can automate this with:

  • -o: Overwrite files without asking
  • -n: Never overwrite existing files
unzip -o project.zip      # Overwrite without prompt
unzip -n project.zip      # Skip existing files

Extract into a separate directory when placement matters

ZIP files are super common when sharing projects or downloading from the internet. Knowing how to use unzip gives you an edge and helps you stay productive in any Linux environment.

Try it, then check the worked answer

Your task: Extract a ZIP into restored.

Use a fresh folder with both zip and unzip installed. The block creates a term folder, saves its class list and builds the input ZIP. No earlier lesson's archive is required. Keep the destination separate from the original tree.

Bash · complete example for this question

mkdir term
printf 'Diya\n' > term/students.txt
zip -r term.zip term
unzip -l term.zip
unzip term.zip -d restored

Expected behavior; machine-dependent details

Listing and extraction messages vary with archive contents.

Worked answer

unzip term.zip -d restored

Use -d restored with the extraction command. unzip -l is useful inspection, but no file is restored by that list-only operation. The ZIP's own name also does not automatically choose your desired destination directory.

Check your decision

Extract a ZIP into restored.
Choose one answer. You can try again.

Answer and explanation

unzip term.zip -d restored

Use -d restored with the extraction command. unzip -l is useful inspection, but no file is restored by that list-only operation. The ZIP's own name also does not automatically choose your desired destination directory.

Listing a ZIP does not extract its members

Read a ZIP member list first, then decide where extraction should write. Check the full restored paths, not just whether a destination folder exists. Keep original and restored copies separate when you want to compare their contents or avoid unintended overwrites.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Restore gzip-Compressed Data →