Zipping folders is a common task when you need to compress data, save disk space, or prepare files for transfer. Ubuntu and Debian systems make this easy with the zip command.
โ Prerequisites
- A terminal session
- Basic Linux user access (non-root or root)
ziputility installed (if not, see below)
๐ง Step 1: Install zip (if not already installed)
To install the zip and unzip utilities on Ubuntu/Debian, run:
sudo apt update
sudo apt install zip unzip
๐ Step 2: Create a Zip File From a Folder
To compress a folder (e.g., myfolder) into a .zip archive, use:
zip -r myfolder.zip myfolder
Explanation:
zip: the command-r: recursively compress all files and subdirectoriesmyfolder.zip: the output zip file namemyfolder: the folder to compress
๐งช Example
zip -r project_backup.zip /home/user/projects/myapp
This will create project_backup.zip containing all files from /home/user/projects/myapp.
๐ Optional Zip Command Flags
| Flag | Description |
|---|---|
-r | Recursively zip directories |
-9 | Use maximum compression |
-e | Encrypt zip file with password |
-q | Quiet mode, suppress output |
Example with encryption and maximum compression:
zip -re9 secure_backup.zip myfolder
๐ List Contents of a Zip File
unzip -l myfolder.zip
๐ Extract a Zip File
unzip myfolder.zip
๐งน Delete Original Folder After Zipping (Optional)
zip -r myfolder.zip myfolder && rm -rf myfolder
โ ๏ธ Be cautious: this removes the original folder after zipping!
๐ Troubleshooting
- Command not found? Install with
sudo apt install zip. - Permission denied? Use
sudoor check folder permissions. - Large folders take too long? Use
-qto suppress output or-0for no compression (faster).
๐งพ Summary
| Task | Command |
|---|---|
| Install zip | sudo apt install zip unzip |
| Zip folder | zip -r name.zip folder |
| List contents | unzip -l name.zip |
| Extract zip | unzip name.zip |
| Zip + delete original | zip -r name.zip folder && rm -rf folder |
