How to Use the Zip Command in Linux to Compress Files
Introduction
The Zip command in Linux is a powerful utility that compresses files and directories into a single archive file using the .zip
format. Compressing files reduces the size and helps to pack multiple files together for faster data transfer or storage. Many operating systems, such as Windows, macOS, Linux, and Unix support the Zip format, allowing you to pack and extract files across major platforms.
This article explains how to use the zip
command in Linux to effectively compress and optimize files.
The zip
Command Syntax in Linux
The following is a basic zip
command syntax:
zip [options] zipfile files
In the above zip
command:
[options]
: Includes optional flags that modify the command's behavior.zipfile
: Specifies the name of the resulting Zip archive.files
: Defines the files or directories you want to compress.
Set Up Sample Files and Directories
Follow the steps below to create sample files and directories to test the zip
command in linux:
Switch to your user's home directory.
console$ cd
Create multiple sample text files. For instance,
file.txt
,file1.txt
,file2.txt
,newfile.txt
, andfile.log
.console$ touch file.txt file1.txt file2.txt newfile.txt file.log
Create sample directories, such as
dir
anddir1
. Then create asub_dir
sub-directory underdir1
.console$ mkdir -p dir dir1/sub_dir
Switch to the
dir
directory.console$ cd dir
Create new files in the directory, such as
file1.txt
,file2.txt
,sys.log
, andauth.log
.console$ touch file1.txt file2.txt sys.log auth.log
Use the Most Common zip
Command Options
The following are the most common zip
command options:
Option | Description |
---|---|
-r |
Recursively compress directories and their contents. |
-j |
Junk (omit) directory names from the Zip archive. |
-m |
Move the specified files into the Zip archive (delete files after zipping). |
-u |
Update existing entries in the Zip archive with newer versions. |
-d |
Delete entries from the Zip archive. |
-x |
Exclude specific files or patterns from being zipped. |
-q |
Quiet mode, suppress output messages for less verbosity. |
-v |
Verbose mode, display detailed output during the compression process. |
-9 |
Use the best compression method to maximize file size reduction. |
-e |
Encrypt the Zip archive with a password for added security. |
Run Practical zip
Command in Linux with Examples
Follow the steps below to run some zip
command examples:
Compress a single
file.txt
file into a Zip archive and name itarchive.zip
.console$ zip archive.zip file.txt
Output:
Compress
file.txt
,file1.txt
, andfile2.txt
files into a single Zip archive. For instance,archive.zip
.console$ zip archive.zip file.txt file1.txt file2.txt
Output:
Compress the
dir
directory and all its contents including sub-directories intoarchive.zip
.console$ zip -r archive.zip dir/
Output:
Exclude specific files from the archive, such as all
.log
files.console$ zip -r archive.zip dir/ -x "*.log"
Output:
Add the
newfile.txt
file to an existingarchive.zip
archive.console$ zip -u archive.zip newfile.txt
Output:
List the contents of the
archive.zip
archive using the-sf
option.console$ zip -sf archive.zip
Output:
Overwrite or update the
archive.zip
archive withfile1.txt
andfile2.txt
. The command replacesfile1.txt
andfile2.txt
in the archive if the files exist.console$ zip -o archive.zip file1.txt file2.txt
Output:
Delete specific files, such as
file1.txt
from thearchive.zip
console$ zip -d archive.zip file1.txt
Output:
Extract files from the
archive.zip
archive.console$ unzip archive.zip
Output:
Specify the
-9
option to compress files with the best compression method and reduce the archive size. The command may take more time to complete.console$ zip -9 archive.zip file1.txt file2.txt
Output:
Compress the
file1.txt
andfile2.txt
files and encrypt thearchive.zip
archive with a password. When prompted, enter a password. You'll need it when unzipping the file.console$ zip -e archive.zip file1.txt file2.txt
Output:
Use Advanced zip
Command Options
Use the
-j
option to compress thefile1.txt
andfile2.txt
files without including the directory paths in the archive.console$ zip -j archive.zip /path/to/file1.txt /path/to/file2.txt
Output:
Select and compress multiple
*.txt
files using the wildcard character*
.console$ zip archive.zip *.txt
Output:
Use the
-v
option to display detailed output when compressing thefile1.txt
andfile2.txt
files.console$ zip -v archive.zip file1.txt file2.txt
Output:
Compress the
file1.txt
andfile2.txt
files intoarchive.zip
archive, encrypt the archive, and display detailed output.console$ zip -e -v archive.zip file1.txt file2.txt
Output:
Compress the
file1.txt
andfile2.txt
files intoarchive.zip
while deleting the source files.console$ zip -m archive.zip file1.txt file2.txt
Output:
Unzip the
archive.zip
archive into thedir1
directory.console$ unzip archive.zip -d dir1/
Output:
Create a new
archive.zip
archive and compress its contents using a different compression level, such as-9
.console$ zip -r -9 archive.zip dir/
Output:
Handle file conflicts in the
archive.zip
archive using theu
option. For instance, the following command updates the archive with newer versions of files to avoid overwriting files in the existing archive.-n .txt
instructs the command only to update the.txt
files.console$ zip -u archive.zip *.txt -n .txt
Output:
Combine zip
with Other Commands
You can combine the zip
command with other commands for advanced use cases as detailed below:
Combine
find
andzip
commands to find and compress files modified in the last 24 hours under the/home/vultr_user/dir
directory. Thezip archive.zip -@
inputs the file paths from thefind
command results and adds them to thearchive.zip
file.console$ find /home/vultr_user/dir -type f -mtime -1 -print | zip archive.zip -@
Output:
Combine
find
andzip
commands to find and compress files larger than a specific size like 10MB.console$ find /home/vultr_user/dir -type f -size +10M -print | zip large_files.zip -@
Output:
Pipe the output of the
ls -l /home/vultr_user/dir
command to thezip
command. The command lists all files in the/home/vultr_user/dir
directory and compresses them intoarchive.zip
. The hyphen-
option allows thezip
command to read from standard input.console$ ls -l /home/vultr_user/dir | zip archive.zip -
Output:
Conclusion
You have used the Zip command in Linux to compress files and directories. The zip
command reduces storage size, improves data transfer, and allows file portability. For more information and configuration options, run man zip
to view the Zip command's manual page.