Ubuntu 22.04 Server / How to Extract ZIP Files

To extract a ZIP file on Ubuntu 22.04 Server, install unzip and then run unzip example.zip. By default, the contents of the ZIP file are extracted to the current working directory, but you can specify another location with the -d option.

Installing unzip and checking file location

unzip is a command to check the contents of a ZIP file and extract it. First, verify if it is installed, and if the command cannot be found, install the package. Updating the package list and installing it requires administrative privileges and access to the package repository.

unzip -v
sudo apt update
sudo apt install unzip

Before extracting, check the current location with pwd and see if the ZIP file exists with ls. In the example below, example.zip should be replaced with the actual file name or path. If the file name contains spaces, enclose it in quotes like "my archive.zip".

Check the list and basic extraction

The following command shows a list of files without extracting them. Check the names, sizes, and folder structure to see where they will be extracted.

unzip -l example.zip

To extract to the current directory, run the following command. If there are subfolders stored in the ZIP, their structure will also be created. If files with the same name already exist, you will be asked whether to overwrite them by default.

unzip example.zip

After completion, check the results with ls. If there is no top-level folder in the ZIP, multiple files may appear directly in the current directory, so it is convenient to extract to a separate location to avoid mixing with existing files.

Extracting to the desired folder

Specify the extraction location after -d. The following example prepares a output directory with write permission and extracts into it.

mkdir -p output
unzip example.zip -d output
ls output

The folder structure stored in the compressed file is maintained under output. When changing the path to another location, make sure you have permission to create files there and enough storage space. Normally, sudo is not required to extract your own files.

Overwriting and extracting some files

If a file with the same name already exists and you want to skip it without confirmation, use -n. Even if the contents of the compressed file are newer, the existing file will be preserved, so make sure the result is what you expect.

unzip -n example.zip -d output

-o overwrites files with the same name without confirmation, so use it carefully only when it's acceptable to change existing files. If you need only specific files rather than the entire archive, you can specify the exact path within the ZIP after the file name.

unzip example.zip "docs/readme.txt" -d output

First, check in the list whether docs/readme.txt actually exists. When using wildcards, it is important to enclose the pattern in quotes to prevent the shell from expanding it first.

If an error occurs

To check the archive for errors, run the following command. -t extracts each entry in memory and compares its calculated CRC with the stored value. Check that it reports no errors. This check does not mean the files are trustworthy or free from malicious content.

unzip -t example.zip
  • file not found: Check the current location, file name, case sensitivity, and path.
  • permission error or insufficient space: Check the write permission and remaining space in the target directory, and if necessary, choose another folder with write access.
  • Damaged or unsupported compression: Make sure the download is complete and try receiving the original file again. Depending on the encryption or compression method, the installed tool may not support it.
  • File name corruption: Check the character encoding of the environment where the ZIP was created and the support of the tool you are using. If there is a file recreated with UTF-8 file names, try comparing it.

Unknown source ZIPs should first be checked for contents and extracted in a separate directory. Checking the contents and integrity does not replace a full security check, and do not execute any scripts or executable files from the archive before verifying their contents. The full set of options can be found in unzip -h or Ubuntu 22.04 unzip documentation.