zip and unzip are command-line utilities for compressing and extracting files and directories on Linux systems. zip combines multiple files and directories into a single compressed archive to save storage space and simplify file management, while unzip extracts the archived contents when needed. This guide covers installing both utilities and using them to compress individual files, archive entire directories, and extract archive contents. By the end, you'll have zip and unzip installed and working archive and extraction workflows for both individual files and whole directory trees.
Prerequisites
- Access to a Linux server as a non-root sudo user
Install Zip and Unzip
The zip and unzip utilities are available through the default package managers on most Linux distributions.
Ubuntu & Debian:
1. Update the apt package index:
$ sudo apt update 2. Install zip and unzip:
$ sudo apt install -y zip unzip CentOS/AlmaLinux/Rocky Linux/Fedora:
$ sudo dnf install -y zip unzip If you're using an older version of Rocky Linux, AlmaLinux, or CentOS that doesn't support dnf, replace dnf with yum in the command above.
Arch Linux:
$ sudo pacman -S zip unzip openSUSE:
$ sudo zypper install -y zip unzip Alpine Linux:
1. Update the apk package index:
$ sudo apk update 2. Install zip and unzip:
$ sudo apk add zip unzip zip Command Syntax
$ zip [OPTIONS]... [COMPRESSED_FILENAME] [FILES/DIRS TO COMPRESS]... Where:
-
[OPTIONS]modify the command behavior and are optional -
[COMPRESSED_FILENAME]is the name of the archive file the command creates -
[FILES/DIRS TO COMPRESS]is the list of files or directories to archive
zip Command Usage
1. Compress a single file into a .zip archive:
$ zip archive-name.zip file1.txt 2. Add multiple files to a new or existing archive:
$ zip archive-name.zip file1.txt file2.txt file3.txt If the archive doesn't exist, this creates a new one; if it exists, it updates the archive with the specified files.
3. Set the compression level (-0 for fastest/no compression, -9 for maximum compression):
$ zip -9 archive-name.zip file.txt 4. Password-protect an archive with the -P option:
$ zip -P your_password archive-name.zip file4.txt 5. Exclude specific files from the archive with the -x flag:
$ zip archive-name.zip *.txt -x secret.txt This adds all .txt files in the directory to the archive except secret.txt.
6. Update files in the archive only if the source file is newer, with the -u flag:
$ zip -u archive-name.zip updatedfile.txt 7. View detailed output while compressing with the -v flag:
$ zip -v archive-name.zip file1.txt unzip Command Syntax
$ unzip [OPTIONS]... [ZIP_FILENAME] [MEMBER FILES/DIRS]... Where:
-
[OPTIONS]modify how the contents are extracted and are optional -
[ZIP_FILENAME]is the.zipfile to extract entirely or partially -
[MEMBER FILES/DIRS]are files or directories within the archive to extract; if omitted, the command extracts everything
unzip Command Usage
1. Extract all files from an archive into the working directory:
$ unzip archive-name.zip 2. Extract a single file instead of the full archive:
$ unzip archive-name.zip file1.txt 3. Extract to a specific destination with the -d flag:
$ unzip archive-name.zip -d /path/to/destination 4. Preview the files inside an archive without extracting with the -l flag:
$ unzip -l archive-name.zip 5. Extract multiple specific files by listing them after the archive name:
$ unzip archive-name.zip file1.txt updatedfile.txt 6. Overwrite existing files automatically with the -o flag:
$ unzip -o archive-name.zip 7. Skip files that already exist in the destination with the -n flag:
$ unzip -n archive-name.zip 8. View detailed output during extraction with the -v flag:
$ unzip -v archive-name.zip Zip a Directory
1. Compress a directory and all its contents with the -r flag:
$ zip -r archive-name.zip directory-name 2. Exclude specific files, file types, or folders during compression with the -x flag:
$ zip -r archive-name.zip directory-name -x "*.log" "*.tmp" "cache/*" 3. Combine multiple directories into a single zip file:
$ zip -r archive-name.zip dir1 dir2 dir3 4. Compress the entire working directory using a single dot:
$ zip -r archive-name.zip . Unzip a Directory
1. Extract a full directory structure into the working directory:
$ unzip archive-name.zip 2. Extract into a specific location with the -d flag:
$ unzip archive-name.zip -d /path/to/destination 3. Automatically overwrite existing files and directories with the -o flag:
$ unzip -o archive-name.zip -d /path/to/destination 4. Skip overwriting existing files or directories with the -n flag:
$ unzip -n archive-name.zip -d /path/to/destination Next Steps
- Automate periodic backups by scripting
zip -ragainst a cron schedule - Combine
zipwithscporrsyncto transfer compressed archives between servers - Explore
tarfor archiving on systems where.tar.gzis the preferred format - Add password protection to any archive that contains sensitive files before sharing it
For the full guide with additional tips, visit the original article on Vultr Docs.