A ZIP file can contain more than you expect, and unzip archive.zip writes its contents into your shell’s current directory—not necessarily beside the archive. Before extracting, check where you are and what the archive contains. That small pause can prevent files from landing in the wrong place or overwriting something important.
Inspect first, extract second
Start by checking your current directory and listing the ZIP’s contents:
pwd
unzip -l backup.zip
pwd shows where files will be written if you extract without specifying a destination. The -l option lists the archive’s entries without extracting them. If you’re not sure what’s already in the current directory, check the directory contents with ls too.
You can also test whether unzip can read the archive’s entries and checksums:
unzip -t backup.zip
A successful test can help identify common archive corruption, but it doesn’t prove the files are safe. For an archive you don’t trust, inspect the listing and extract into a separate directory rather than somewhere containing important files.
Extract into a dedicated directory
The basic command is:
unzip backup.zip
To choose a destination, use -d:
unzip backup.zip -d ~/extracted/backup
unzip can create the destination directory if it doesn’t exist. If you want to create it explicitly first—for example, to control when and where the directory is made—use mkdir -p:
mkdir -p ~/extracted/backup
unzip backup.zip -d ~/extracted/backup
The -p option creates missing parent directories as needed. See this guide to creating directories with mkdir for more on directory paths and options.
The location of the ZIP and the extraction destination are independent. For example, this command reads an archive from ~/Downloads but extracts it under /tmp:
unzip ~/Downloads/site-files.zip -d /tmp/site-files
Handle existing files deliberately
If the destination already contains a file with the same name as an archive entry, unzip normally asks whether to replace it. Replacing a file overwrites the existing copy, so pay attention to the prompt.
Two useful options change that behavior:
unzip -n backup.zip -d ~/extracted/backup
unzip -o backup.zip -d ~/extracted/backup
-n skips files that already exist. -o overwrites existing files without prompting. Use -o only when replacing those files is intentional; it removes the chance to review each conflict interactively. When in doubt, extract into a new, empty directory instead.
Extract only what you need
After checking the archive’s internal paths with unzip -l, you can name a particular entry to extract it:
unzip backup.zip "reports/summary.txt" -d ~/extracted/backup
The path must match the name stored inside the ZIP. Quotes are useful when a name contains spaces or shell wildcard characters, because they prevent the shell from interpreting those characters before unzip sees them.
To ignore the directory paths stored in the archive and put files directly into the destination, use -j:
unzip -j backup.zip -d ~/extracted/flat
Be careful with this option. If different directories in the archive contain files with the same basename, flattening removes the path distinction and can create filename conflicts. Inspect the listing first.
When unzip is missing
If the shell prints unzip: command not found, install the package using your distribution’s package manager. Common examples are:
# Debian or Ubuntu
sudo apt update && sudo apt install unzip
# Fedora, RHEL, or Rocky Linux
sudo dnf install unzip
# Arch Linux
sudo pacman -S unzip
Package-manager versions and package availability can vary by distribution and release. If one of these commands isn’t available, check which package manager your system uses.
ZIP isn’t the same as gzip
Linux has several compressed-file formats, and their extensions matter. unzip is for ZIP archives. A .tar.gz or .tgz file is a tar archive compressed with gzip; a standalone .gz is typically one compressed file.
# Extract a .tar.gz archive into a chosen directory
mkdir -p /tmp/archive-files
tar -xzf archive.tar.gz -C /tmp/archive-files
# Decompress a standalone .gz file
gunzip file.gz
The usual gunzip file.gz command replaces the compressed file with its decompressed output. Don’t assume a file should be handled by unzip just because it is compressed.
A quick safety checklist
Before extracting an unfamiliar ZIP:
- Run
pwdto confirm your current directory. - Use
unzip -l archive.zipto inspect the archive’s paths and filenames. - Choose a dedicated destination with
-d. - Avoid
-ounless you mean to replace conflicting files. - Use
unzip -t archive.zipas a check for common archive errors—not as a safety guarantee.
For a routine extraction, unzip archive.zip -d /path/to/destination makes the output location explicit. That’s often the simplest way to keep an archive’s contents separate from the files you’re already working with.
I originally published a more detailed version of this guide on the SSHFlow blog.
I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.
Top comments (0)