Cantech Knowledge Base

Your Go-To Hosting Resource

How to Use the Zip Command in Linux to Compress Files?

To use the zip command in Linux, run zip [options] zipfile files — for example, zip -r archive.zip foldername/ compresses a folder into a single .zip archive. This reduces file size for storage and sharing, and works across Windows, macOS, and Linux. Common options include -r for folders, -e for encryption, and -x to exclude 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

Option Function Example
-r Compress an entire directory and its subdirectories zip -r archive.zip dir/
-j Excludes directory paths from the compressed file zip -j archive.zip /path/to/file.txt
-m Moves files into the zip archive (deletes originals after zipping) zip -m archive.zip file.txt
-u Updates an existing archive with newer versions of files zip -u archive.zip newfile.txt
-d Removes specific files from a zip archive zip -d archive.zip file1.txt
-x Excludes certain files from being zipped zip -r archive.zip dir/ -x "*.log"
-q Quiet mode — hides messages while compressing zip -q archive.zip file.txt
-v Verbose mode — shows detailed compression info zip -v archive.zip file.txt
-9 Applies maximum compression zip -9 archive.zip file.txt
-e Encrypts the archive with a password zip -e archive.zip file.txt

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.

FAQs

What is the zip command in Linux?

The zip command is a Linux utility used to compress one or more files or directories into a single .zip archive. It reduces file size for storage or transfer and is compatible with Windows, macOS, and Unix-based systems.

Is zip installed by default on Linux?

No, zip is not always pre-installed. On Debian/Ubuntu, install it with sudo apt install zip. On CentOS/RHEL, use sudo yum install zip. To unzip archives, you also need unzip installed separately (sudo apt install unzip).

How do I check the installed zip version?

Run zip -v in the terminal. This displays the installed version along with compression details and supported features.

What is the difference between zip and tar in Linux?

zip compresses and archives files into a single step, producing a .zip file readable across all major operating systems. tar (often combined with gzip as .tar.gz) is more common for Linux backups and preserves Unix file permissions more reliably, but .tar.gz files aren’t natively supported on Windows without extra software.

How do I unzip a file in Linux?

Use unzip archive.zip to extract contents into the current directory, or unzip archive.zip -d /target/folder to extract into a specific folder.

How do I zip a folder including all its subfolders?

Use the -r (recursive) flag: zip -r archive.zip foldername/. This compresses the folder along with all files and subdirectories inside it.

How do I password-protect a zip file?

Use the -e flag: zip -e archive.zip file.txt. The command will prompt you to set and confirm a password before creating the encrypted archive.

How do I exclude certain files while zipping?

Use the -x flag followed by the pattern to exclude. For example, zip -r archive.zip dir/ -x “*.log” compresses everything in dir/ except .log files.

Can I zip files found by another command, like find?

Yes. Pipe the output into zip using -@. For example: find /home/user/dir -type f -mtime -1 -print | zip archive.zip -@ zips all files modified in the last 24

July 31, 2026