Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Unzip a File in Ubuntu

sudo apt install unzip
unzip -l archive.zip
unzip archive.zip -d ~/archive
unzip archive.zip 'config/*'

Install unzip once, list the archive to see what it holds, extract it into a folder of your choice with -d, or extract only the entries that match a pattern. Without -d, unzip extracts into the current folder.

Install unzip

Ubuntu and Debian server images do not always include unzip. Install it with sudo apt install unzip; on AlmaLinux and Rocky Linux, run sudo dnf install unzip. To create archives as well, install the zip package; see how to zip files in Linux.

Look Inside Before You Extract

unzip -l archive.zip
unzip -t archive.zip

-l lists names, sizes, and dates without writing anything. Check whether everything sits inside one top folder; if not, extracting into your current folder mixes the archive's files with yours. -t tests every entry and reports damaged ones, which is useful after a download.

Extract to a Directory

unzip release.zip -d /tmp/release-check
ls -la /tmp/release-check

-d names the target folder, and unzip creates it if it does not exist. Extracted files belong to the user who runs unzip, so run it as the user who should own them. If you extracted as root, set the owner afterward with chown.

Never Unzip an Untrusted Archive into a Web Root

A zip file can contain scripts, .htaccess files, and files whose names match ones you already have. Extracted straight into /var/www, a PHP file from an unknown archive can be reached from the web and runs on your server at the first request. Use this order:

  1. List the archive with unzip -l and read the names.
  2. Extract it into a new, empty folder outside the web root with -d.
  3. Check the files, hidden ones included, with ls -la or find.
  4. Take a snapshot or backup of the site, then copy only what you need into place.

By default, unzip removes ../ parts and a leading / from entry names, so an archive cannot write outside the target folder that way. It can still replace files inside it, and it can contain symbolic links that point elsewhere, which is one more reason to check the files before you copy them.

Overwriting Existing Files

OptionBehavior
noneAsks for each existing file
-oOverwrites without asking
-nNever overwrites
-uReplaces older files, adds new ones
-fReplaces older files only

Without an option, unzip stops at each file that already exists and asks what to do, with answers for yes, no, all, none, and rename. In scripts, always pass -o or -n, so the command does not wait for an answer that never comes.

Extract One File or Leave Some Out

unzip backup.zip site/config.php
unzip backup.zip '*.sql' -d ~/sql
unzip backup.zip -x '*.log'
unzip -j backup.zip 'site/images/*' -d ~/images
unzip -p backup.zip site/config.php | less

Give the path as unzip -l prints it. Quote patterns so unzip, not the shell, matches them. -x leaves entries out, -j drops the folder paths and puts every file directly in the target, and -p prints a file to the screen without writing it to disk.

Password-Protected Archives

For an encrypted archive, unzip asks for the password. Type it at the prompt and avoid -P, which leaves the password in your shell history and in the process list. unzip handles only the classic ZIP encryption. Archives encrypted with AES, which some archive tools create, fail with "unsupported compression method 99". On Debian and Ubuntu, install the 7zip package (in the universe component on Ubuntu) and extract them with 7z x archive.zip; on Debian 12 the command is 7zz.

Common Errors

  • End-of-central-directory signature not found: the file is not a zip archive, or the download is incomplete. file archive.zip shows what it really is; "HTML document" means the download saved an error page.
  • Cannot find or open the archive: wrong name or folder. Check with ls -l and see No such file or directory.
  • Disk full: check the available space with df -h before extracting large archives.
  • The file ends in .tar.gz: it is not a zip archive; see how to extract tar.gz files.
Was this answer helpful?
Loading...