Extracting a ZIP file to a specified folder

Linux 3 min read

A common task is to extract a downloaded or uploaded ZIP file (such as a WordPress theme, a website backup or an application installation package) into a specific directory on the server. You do not need to download the file to your own computer to do this: whilst logged in via SSH, you can extract it to the selected folder directly from the command line.

Installing unzip

ZIP files are extracted using the unzip command. It is installed by default on many systems, but is often missing from minimal server installations; for example, neither unzip nor zip was present on the AlmaLinux 10 cloud image. If you receive a command not found error, install it:

sudo apt install unzip      # Debian, Ubuntu
sudo dnf install unzip      # AlmaLinux, Rocky Linux

sudo is not available on shared hosting; unzip is usually already available there.

Extract to a specified folder

By default, the unzip command extracts files to the current directory. You can specify the destination folder using the -d switch:

unzip fontosfajl.zip -d /var/www/teszt

If the destination folder does not exist, the unzip command will create it, but only the top-level directory. If the parent directories do not exist either, the command will terminate with the error checkdir: cannot create extraction directory: … No such file or directory (this was the case in the test). In this instance, first create the full path using the mkdir -p command:

mkdir -p /var/www/teszt/uj/mappa
unzip fontosfajl.zip -d /var/www/teszt/uj/mappa

View content before unpacking

It is worth checking the archive first, as many ZIP files contain their own root folder (for example, sablon/), meaning the files will be located one level deeper than you might expect:

unzip -l fontosfajl.zip
  • -o: overwrites existing files without prompting.
  • -n: it never overwrites existing files, but only extracts the missing ones.
  • -q: quiet mode; does not list files individually.
  • -j: ignores the directory structure; all files are placed directly into the destination folder.
  • Extracting a single file: unzip fontosfajl.zip wp-config.php -d /tmp/. Specify the file using exactly the same path as listed by unzip -l: if the archive contains a root folder, for example oldal/wp-config.php. With the -j switch, the file is placed directly into the destination folder without the folder structure.

If you do not specify the -o or -n option, and a file with the same name already exists in the destination directory, unzip will prompt you for each conflict ([y]es, [n]o, [A]ll, [N]one, [r]ename).

Verification and permissions

After extraction, you can check the files using the command ls -la /var/www/teszt. If you extracted the files into a website directory as the root user, the files will be owned by root and the web server will not be able to write to them. Set the correct owner and permissions using the chmod and chown commands.

Common errors

  • End-of-central-directory signature not found: the file is corrupted, has not been fully downloaded, or may not even be in ZIP format. The command file fontosfajl.zip reveals its actual type: if, for example, it is an HTML document, the download returned an error page instead of the file. However, a truncated ZIP filee is also identified as a ZIP by the file (in the test, even a halved archive was labelled ‘Zip archive data’); in this case, compare its size with the original and download it again.
  • Out of space: the extracted content may be significantly larger than the ZIP file. Before proceeding, check the available space using the df -h command.

Our article covers the management of .tar.gz archives, including how to create and extract them.

Back: Linux articles (62)