Zip a folder with zip -r archive.zip folder and extract it with unzip archive.zip. For server backups, tar.gz is often the better choice.
Install zip and unzip
Check whether they're there:
zip -v | head -n 2
unzip -v | head -n 1
If you see "command not found", install them. Ubuntu:
sudo apt install zip unzip
AlmaLinux, Rocky Linux or CloudLinux:
sudo dnf install zip unzip
Zip a folder
Go to the folder that contains the one you want to zip, then zip it. -r includes everything inside, with all subfolders:
cd /var/www/example.com
zip -r site-backup.zip public_html
This creates site-backup.zip with the folder public_html inside.
More than one folder or file in one archive:
zip -r backup.zip public_html logs notes.txt
Leave a folder out, for example a cache folder:
zip -r site-backup.zip public_html -x "public_html/wp-content/cache/*"
Check what's in the archive:
unzip -l site-backup.zip
Unzip a file
Extract into the current folder:
unzip site-backup.zip
Extract into another folder (it's created if it doesn't exist):
unzip site-backup.zip -d /var/www/example.net
If files with the same names exist, unzip asks for each one. Add -o to overwrite all without asking, or -n to never overwrite:
unzip -o site-backup.zip -d /var/www/example.net
Test an archive for damage without extracting it:
unzip -t site-backup.zip
Use tar.gz instead
A .tar.gz file keeps Linux file permissions and owners, and tar is installed on every Linux server. Use it for backups and moves between servers; use zip when someone needs to open the file on Windows.
tar -czf site-backup.tar.gz public_html # create
tar -tzf site-backup.tar.gz | head # list the contents
tar -xzf site-backup.tar.gz # extract here
tar -xzf site-backup.tar.gz -C /var/www/example.net # extract into an existing folder
In -czf, c means create, z compress with gzip and f the file name that follows. x extracts and t lists.
Common problems
"zip warning: name not matched"
The folder name is wrong or you're in the wrong place. Run pwd to see where you are and ls to see the names, then try again.
The website shows errors after extracting as root
Files extracted by root belong to root, and the website can't change them. On a cPanel server, give them back to the account (replace username with the cPanel user name):
chown -R username:username /home/username/public_html
On a server without cPanel, use the user that owns the site, for example sudo chown -R $USER:$USER /var/www/example.com/public_html.
"No space left on device"
The archive or the extracted files don't fit. Check free space with df -h, delete what you don't need (old archives, for example) and try again.
The zip file needs a password
zip -e sets a password, but zip's built-in encryption is weak. Don't rely on it for sensitive data; keep such archives off public folders.
Related articles
- Connect to your server with SSH
- Host several websites on one server with Apache
- Install the Apache web server on Ubuntu
Still stuck? Open a support ticket and we'll help.