
zip
and unzip
are popular command-line utilities for compressing and extracting files and directories on Linux systems. zip
combines multiple files and directories into a single compressed archive, saving storage space and simplifying file management. unzip
extracts the archived contents when needed.
This article explains how to install Linux's zip
and unzip
utilities to compress files, archive directories, and extract the archived contents.
Prerequisites
Before you begin, you need to:
- Have access to a Linux instance as a non-root sudo user.
Install Zip and Unzip in Linux
The zip
and unzip
utilities are available through the default package managers on most Linux distributions. Follow the steps below to install both tools using the package manager that matches your distribution.
Follow the steps below to install zip
and unzip
on Debian-based distributions.
Update the
apt
package index.console$ sudo apt update
Install
zip
andunzip
.console$ sudo apt install -y zip unzip
Run the following command to install
zip
andunzip
on RHEL-based distributions.console$ sudo dnf install -y zip unzip
dnf
, replace dnf
with yum
in the installation command.
Run the following command to install
zip
andunzip
on Arch Linux.console$ sudo pacman -S zip unzip
Run the following command to install
zip
andunzip
on openSUSE.console$ sudo zypper install -y zip unzip
Follow the steps below to install zip
and unzip
on Alpine Linux.
Update the
apk
package index.console$ sudo apk update
Install
zip
andunzip
.console$ sudo apk add zip unzip
Zip Files in Linux
The zip
command to create a .zip
archive file that reduces file size and simplifies storage or file sharing.
zip
Command Syntax
The zip
command uses the following syntax.
zip [OPTIONS]... [COMPRESSED_FILENAME] [FILES/DIRS TO COMPRESS]...
Where,
[OPTIONS]
modify the command behavior. These are optional. The next section discusses and demonstrates some important options.[COMPRESSED_FILENAME]
is the name of the file that the command should create after archiving the files.[FILES/DIRS TO COMPRESS]
is the list of files or directories you want to archive.
zip
Command Usage
Compress a single file into a
.zip
archive.console$ zip archive-name.zip file1.txt
The above command creates an archive named
archive-name.zip
that containsfile1.txt.
Add multiple files to a new or existing archive.
console$ zip archive-name.zip file1.txt file2.txt file3.txt
The above command adds the listed files to
archive-name.zip
. If the archive doesn’t exist, it creates a new one. If it exists, it updates the archive with the specified files.Set the compression level. Use
-0
to speed up the zip process without compression. Use-9
to compress the file as much as possible.console$ zip -9 archive-name.zip file.txt
Use the
-P
option to set a password on a zip archive. Password-protecting archives can help secure sensitive files during storage or transfer.console$ zip -P your_password archive-name.zip file4.txt
The above command creates an
archive-name.zip
file containingfile4.txt
, protected by the passwordyour_password
.Use the
-x
flag to exclude specific files from the archive. This is useful when compressing multiple files using a wildcard pattern and you want to skip specific files.console$ zip archive-name.zip *.txt -x secret.txt
The above command adds all
.txt
files in the directory to the archive exceptsecret.txt.
Use the
-u
flag to update files in the archive only if the source file is newer.console$ zip -u archive-name.zip updatedfile.txt
The above command updates
updatedfile.txt
in the archive if the local version has changed since the last zip.Use the
-v
flag to view detailed output while compressing files. The verbose output shows the filename, compression method, and size for each file added to the archive.console$ zip -v archive-name.zip file1.txt
Unzip Files in Linux
Use the unzip command to extract files from a .zip
archive. You can extract all files, target specific files, or list the zipped files.
unzip
Command Syntax
The unzip
command uses the following syntax.
unzip [OPTIONS]... [ZIP_FILENAME] [MEMBER FILES/DIRS]...
Where,
[OPTIONS]
modify how the contents are extracted. This is optional. The most important options are covered in the next section.[ZIP_FILENAME]
is the.zip
file you want to extract entirely or some of its files.[MEMBER FILES/DIRS]
are files or directories within the archive to extract. If omitted, the command extracts all files.
unzip
Command Usage
Run the unzip command followed by the archive name to extract all files into the working directory.
console$ unzip archive-name.zip
Specify the filename after the archive name to extract only that file instead of the full archive.
console$ unzip archive-name.zip file1.txt
The above command extracts only
file1.txt
from the archive and puts it into the working directory.Use the
-d
flag to define the destination path where you want the files to be extracted. This is helpful when you want to keep extracted files organized.console$ unzip archive-name.zip -d /path/to/destination
The above command extracts all files from the archive into the
/path/to/destination
directory.Use the
-l
flag to preview all files inside the archive. This option lets you inspect the archive contents before extracting.console$ unzip -l archive-name.zip
List multiple filenames after the archive name to extract only those files from the archive.
console$ unzip archive-name.zip file1.txt updatedfile.txt
The above command extracts
file1.txt
andupdatedfile.txt
from the archive and puts them into the working directory.Use the
-o
flag to overwrite files that already exist in the destination without prompting. Overwriting files automatically is useful when scripting or automating the extraction process.console$ unzip -o archive-name.zip
The above command extracts all files from
archive-name.zip
and replaces any existing files with the same names without asking for confirmation.Use the
-n
flag to skip files already present in the destination. Skipping existing files prevents overwriting.console$ unzip -n archive-name.zip
The above command extracts files from
archive-name.zip
but skips any files that already exist in the destination directory.Use the
-v
flag to view detailed output during extraction. This includes file names, sizes, and timestamps for each extracted file.console$ unzip -v archive-name.zip
Zip a Directory
You can archive entire directories into a single .zip
file to simplify backups, organize project files, or reduce storage usage.
Use the
zip
command with the-r
flag to compress a directory and include all its files and subdirectories.console$ zip -r archive-name.zip directory-name
The above command creates an archive named
archive-name.zip
containing everything insidedirectory-name
, including nested folders.Use the
-x
flag to exclude specific files, file types, or folders during compression. This helps reduce archive size and avoids including unnecessary files.console$ zip -r archive-name.zip directory-name -x "*.log" "*.tmp" "cache/*"
The above command compresses the
directory-name
but skips all.log
and.tmp
files and the entirecache
folder..List multiple directories after the archive name to combine them into a single zip file.
console$ zip -r archive-name.zip dir1 dir2 dir3
Use a single dot
.
to reference the working directory and compress all its contents.console$ zip -r archive-name.zip .
Unzip a Directory
You can extract a .zip
archive that contains an entire folder hierarchy, including subdirectories and nested files. Extracting complete folder structures is helpful when restoring backups, deploying packaged projects, or accessing compressed folders shared between systems.
Use the
unzip
command followed by the archive name to extract the full directory structure into your working directory.console$ unzip archive-name.zip
Use the
-d
flag to extract the archive into a specific location. This helps keep extracted files organized when working with multiple archives.console$ unzip archive-name.zip -d /path/to/destination
The above command extracts the entire directory structure into
/path/to/destination
.Use the
-o
flag to automatically overwrite the existing files and directories in the target location.console$ unzip -o archive-name.zip -d /path/to/destination
Use the
-n
flag to skip overwriting existing files or directories.console$ unzip -n archive-name.zip -d /path/to/destination
The above command extracts everything from
archive-name.zip
but leaves existing files and folders untouched in the destination directory.
Conclusion
You have installed the zip
and unzip
utilities on your Linux system. These utilities allow you to compress individual files, archive entire directories, and extract files or full folder structures from .zip
archives. For additional options and advanced usage details, refer to the Info-ZIP documentation.
No comments yet.