How to Use the Zip Command in Linux to Compress Files?
Introduction
File compression is an easy-to-use excellent tool with which you can reduce file sizes or bundle multiple files together for easy sharing or storage. The zip command is one of the most popular ways to do this. What does it do? Well, as the command suggests, it creates compressed files in .zip format. This format is widely supported across operating systems like Windows, macOS, and Unix-based systems.
With this article, you will get better at handling compressed files. We provide everything you need to know about using the zip command effectively.
Understanding the zip Command
The basic structure of the zip command is shown as follows –
zip [options] zipfile files
Below is what each part means –
- [options] means optional flags that modify how the command behaves.
- zipfile is the name of the compressed file you are creating.
- files = files or directories you want to compress.
Now, we will learn to create some test files and folders to practice with –
Setting Up Sample Files and Directories
Before we try the zip command, we will prepare a few sample files and directories. Below are the steps for the same –
- Move to your home directory
cd
- Create some text files with –
touch file.txt file1.txt file2.txt newfile.txt file.log
- Create directories and subdirectories –
mkdir -p dir dir1/sub_dir
- Move into the dir folder –
cd dir
- Create more files inside the dir folder –
touch file1.txt file2.txt sys.log auth.log
Now we have our test environment. Next, we will discuss different ways to use the zip command –
Most Useful zip Command Options
Here are some of the most commonly used options when working with zip –
- -r – Compress an entire directory and its subdirectories.
- -j – Excludes directory paths from the compressed file.
- -m – Move files into the zip archive (deletes original files).
- -u – Update an existing archive with newer versions of files.
- -d – Remove specific files from a zip archive.
- -x – Exclude certain files from being zipped.
- -q – Quiet mode (hides messages while compressing).
- -v – Verbose mode (shows detailed compression info).
- -9 – Apply maximum compression.
- -e – Encrypt the archive with a password.
Practical Examples of Using the zip Command
=> To compress a single file named file.txt into a .zip file, use the below command –
zip archive.zip file.txt
=> Compress multiple files such as add file.txt, file1.txt, and file2.txt into one archive –
zip archive.zip file.txt file1.txt file2.txt
=> Compress/zip an entire folder with all its contents with –
zip -r archive.zip dir/
=> Exclude some files such as zip dir/ but skip all .log files –
zip -r archive.zip dir/ -x "*.log"
=> Add files to an existing Zip archive –
E.g. add newfile.txt to archive.zip with –
zip -u archive.zip newfile.txt
=> See the contents of a Zip archive
View what is inside archive.zip with the command –
zip -sf archive.zip
=> Update files in a Zip archive overwriting the files existing
E.g. Replace existing file1.txt and file2.txt in the archive with the below –
zip -o archive.zip file1.txt file2.txt
=> Delete a file from a Zip archive
Remove file1.txt from archive.zip with –
zip -d archive.zip file1.txt
=> Extract/Unzip a Zip archive
unzip archive.zip
=> Compress with the best possible compression level –
zip -9 archive.zip file1.txt file2.txt
=> Encrypt or password protect a Zip archive –
zip -e archive.zip file1.txt file2.txt
Advanced zip Command Techniques
=> Zip files but remove directory paths
E.g. zip file1.txt and file2.txt without directory paths:
zip -j archive.zip /path/to/file1.txt /path/to/file2.txt
=> Compress multiple files with Wildcard
Below command zip all .txt files in the archive called ‘archive.zip’ –
zip archive.zip *.txt
=> Zip files and see the details of the compression process –
zip -v archive.zip file1.txt file2.txt
=> Encrypt and show detailed compression output –
zip -e -v archive.zip file1.txt file2.txt
=> Move files into an archive (removing original files after zipping) –
zip -m archive.zip file1.txt file2.txt
=> Extract a Zip file to a specific folder
The below command unzips archive.zip into the dir1 folder –
unzip archive.zip -d dir1/
=> Apply the highest compression level on a Directory –
E.g. Zipping dir/ with maximum compression –
zip -r -9 archive.zip dir/
=> Update archives without overwriting specific files
E.g. the given command will update only .txt files inside an archive:
zip -u archive.zip *.txt -n .txt
Combining zip with Other Linux Commands
- Find and zip files with respective commands the files modified in the last 24 hours
In this command, the file path results from ‘find’ are added to archive.zip through the zip archive.zip -@ –
find /home/user/dir -type f -mtime -1 -print | zip archive.zip -@
- Similarly, you can find large files of more than 10 MB and zip them
find /home/user/dir -type f -size +10M -print | zip large_files.zip -@
- Create a compressed archive of all files in the /home/user/dir directory using the ‘ls -l’ command and send its output to zip.
This will compress all the listed files into a zip archive named archive.zip. The hyphen (-) option ensures that the zip command reads the file list from the standard input.
ls -l /home/user/dir | zip archive.zip -
Conclusion
The zip command in Linux helps you compress and organize files. It saves storage space and speeds up file transfers too. It has various options with which you can carry out simple to advanced actions. For more details, check the manual with the man zip command.