How to zip and unzip files and folders on your remote server

Moving thousands of small files is slow. Put them in one archive first, transfer the archive, then extract it on the server.

This guide covers two common formats:

  • .zip works well across Linux, macOS, and Windows
  • .tar.gz is common on Linux and usually preserves Unix file permissions

Only extract archives you trust.

Connect to the server with SSH

Log in with your server username and hostname:

ssh [email protected]

If your host gave you a custom port or a specific private key, pass those too:

ssh -p 5555 -i ~/.ssh/id_ed25519 [email protected]

The custom port is a server setting, not one you can choose in the command.

Create and extract ZIP archives

On Ubuntu or Debian, install both ZIP commands with:

sudo apt update
sudo apt install zip unzip

Create an archive from one file:

zip database.zip database.sql

Use -r to include an entire directory and everything inside it:

zip -r project.zip project/

List an archive before extracting it:

unzip -l project.zip

Extract it into the current directory:

unzip project.zip

Or extract it into a separate directory:

unzip project.zip -d project-files

Create and extract .tar.gz archives

tar and gzip do two different jobs. tar bundles files into one archive; gzip compresses that archive. The result is a .tar.gz file, also called a tarball.

Create a compressed archive from a directory:

tar -czf project.tar.gz project/

The flags mean:

  • -c: create an archive
  • -z: compress it with gzip
  • -f: use the following archive filename

Add -v if you want tar to print every file it processes:

tar -czvf project.tar.gz project/

List the contents without extracting them:

tar -tzf project.tar.gz

Extract into the current directory:

tar -xzf project.tar.gz

Or extract into an existing directory:

mkdir -p project-files
tar -xzf project.tar.gz -C project-files

Here, -x means extract and -C selects the destination directory.

Transfer the archive to the server

Run scp on your local computer to upload an archive over SSH:

scp project.tar.gz [email protected]:/var/www/

For a custom SSH port, scp uses an uppercase -P:

scp -P 5555 project.tar.gz [email protected]:/var/www/

Then connect with SSH, change to the upload directory, and extract the archive.

What if you only have FTP or SFTP access?

FTP and SFTP transfer files. They do not let you run zip, unzip, or tar on the server.

Check whether your hosting control panel has an Extract or Unarchive action. If it does not, extract the archive on your computer and upload the individual files.

Avoid uploading a public PHP “unzipper” script. It creates an unnecessary way for attackers to write files on your server. Ask your hosting provider to extract the archive or enable SSH access instead.