[Linux] Extract and Inspect ZIP Files with the unzip Command

目次

Overview

The unzip command is used to extract files from archives compressed in the ZIP format.

Beyond just restoring files, this command allows you to list the contents without extracting or pick specific files to retrieve. It is frequently used when handling data created in Windows environments.

Specifications (Arguments and Options)

Syntax

unzip [options] [zip_filename]

Main Arguments and Options

OptionDescription
-d [dir]Extracts files into the specified directory.
-lLists archive contents without extracting (list).
-tTests archive integrity without extracting (test).
-pExtracts files to standard output instead of disk (for pipes).
-x [file]Excludes specific files from extraction.
-fUpdates existing files only if the version in the archive is newer.
-cDisplays file contents on standard output along with filenames.
-zDisplays only the archive comment.
-ZDisplays detailed archive information (compression ratio, permissions, etc.).

Basic Usage

It is a safe practice to check the contents with the -l option before extracting to see what is inside.

List contents (No extraction)

unzip -l material_pack.zip

Example Output

Archive:  material_pack.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     1024  2025-10-05 14:30   readme.txt
    54321  2025-10-05 14:31   images/logo.png
    20480  2025-10-05 14:32   images/bg.jpg
---------                     -------
    75825                     3 files

After confirming the contents, run the command without options to extract everything to the current directory.

Extract all to current directory

unzip material_pack.zip

Practical Commands

Extract to a Specific Directory

If a ZIP file does not contain a root folder, it can clutter your current directory. Use the -d option to specify an output folder.

# Create 'restored_data' and extract files there
unzip backup_data.zip -d restored_data/

Extract Only a Specific File

You can restore a single necessary file from a large archive.

# Extract only 'readme.txt' from the archive
unzip source_code.zip readme.txt

Exclude Specific Files During Extraction

This is useful for excluding system files (like __MACOSX or .DS_Store) often found in ZIP files created on a Mac.

# Extract while excluding junk files
unzip project_files.zip -x "__MACOSX/*" "*.DS_Store"

Customization Points

Pass File Content Directly to a Pipe (-p)

Use this when you want to search within a file without saving it to the disk.

# Search for "Error" in a log file inside the archive without extracting
unzip -p server_logs.zip "app.log" | grep "Error"

Skip Overwrite Confirmation (-o / -n)

By default, unzip asks if you want to replace existing files. You can automate this with -o (overwrite all) or -n (never overwrite).

# Overwrite all files without confirmation
unzip -o update_patch.zip

Important Notes

  • Default Overwrite Behavior: Without specific options, unzip will prompt you for every duplicate file (replace? [y]es, [n]o...). This can stop batch processes or scripts.
  • Filename Encoding Issues: ZIP files created in Windows (Shift_JIS) may show garbled filenames when extracted in Linux (UTF-8). Some distributions allow you to specify the character code with -O cp932, but this is not always available.
  • Path Traversal: Malicious ZIP files might contain paths like ../../etc/passwd. While unzip usually warns or disables this, it is recommended to check the paths with -l for untrusted files.

Application

Perform an Archive Integrity Check

Confirm if a file was downloaded correctly or if it is corrupted before extracting.

# Run an integrity test without extracting
unzip -t download_archive.zip

Example Output

Archive:  download_archive.zip
    testing: setup.sh                 OK
    testing: bin/app                  OK
No errors detected in compressed data of download_archive.zip.

Summary

The unzip command is an essential tool for exchanging data in a Linux environment. Using -l to check contents and -d to specify an extraction directory are fundamental techniques for keeping your workspace clean. Make it a habit to inspect what is inside before extracting files to an organized location.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次