Working with ZIP Archives in Linux Command Prompt
ZIP is one of the most popular archiving formats. Unlike a home PC that allows you to work with such archives in a convenient graphical interface, most servers don’t provide such an opportunity. So, be sure to know the basic commands to perform typical tasks via a command prompt.
Before You Begin
Make sure that you have all the required packages installed.
For Debian and Ubuntu:
sudo apt update && sudo apt install zip unzip
For CentOS and Rocky Linux/AlmaLinux:
sudo yum makecache && sudo yum install zip unzip
How to Create a ZIP Archive
Use the following command to create an archive:
zip -r archive_name.zip /directory/path/
The -r
is used to recursively add all files and subdirectories to the archive.
How to extract a ZIP Archive
To extract zip archive you can use the following command:
unzip archive.zip
The contents will be unzipped in the current directory by default. If you want to unzip the archive into another place, use the -d option.
For example:
unzip backup.zip -d /home/user/backup/
Additional Useful Options
- -l — show a list of files within the archive without unzipping it:
unzip -l archive.zip
- -u — update a file within the archive:
zip -u archive.zip new_file.txt
Consider the following details when updating a file within the archive:
- If the archive contains no new_file.txt, it will be added.
- If the archive already contains a file of the same name, but its contents on the drive have changed, then this file will be updated to the latest version.
You can also use this command to update multiple files at once, for example:
zip -u archive.zip *.txt
This command will update all .txt files in the archive and will add new ones if they haven’t been added before.
- -e — set a password for the archive:
zip -e archive.zip /file/path