How to Use the Zip Command in Linux to Compress Files

Updated on October 11, 2024
How to Use the Zip Command in Linux to Compress Files header image

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:

  1. Switch to your user's home directory.

    console
    $ cd
    
  2. Create multiple sample text files. For instance, file.txt, file1.txt, file2.txt, newfile.txt, and file.log.

    console
    $ touch file.txt file1.txt file2.txt newfile.txt file.log
    
  3. Create sample directories, such as dir and dir1. Then create a sub_dir sub-directory under dir1.

    console
    $ mkdir -p dir dir1/sub_dir
    
  4. Switch to the dir directory.

    console
    $ cd dir
    
  5. Create new files in the directory, such as file1.txt, file2.txt, sys.log, and auth.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:

  1. Compress a single file.txt file into a Zip archive and name it archive.zip.

    console
    $ zip archive.zip file.txt
    

    Output:

    Single File Compression

  2. Compress file.txt, file1.txt, and file2.txt files into a single Zip archive. For instance, archive.zip.

    console
    $ zip archive.zip file.txt file1.txt file2.txt
    

    Output:

    Multiple Files Compression

  3. Compress the dir directory and all its contents including sub-directories into archive.zip.

    console
    $ zip -r archive.zip dir/
    

    Output:

    Directory Compression

  4. Exclude specific files from the archive, such as all .log files.

    console
    $ zip -r archive.zip dir/ -x "*.log"
    

    Output:

    Excluding Files

  5. Add the newfile.txt file to an existing archive.zip archive.

    console
    $ zip -u archive.zip newfile.txt
    

    Output:

    Updating Zip Archive

  6. List the contents of the archive.zip archive using the -sf option.

    console
    $ zip -sf archive.zip
    

    Output:

    Zip -sf Show Files

  7. Overwrite or update the archive.zip archive with file1.txt and file2.txt. The command replaces file1.txt and file2.txt in the archive if the files exist.

    console
    $ zip -o archive.zip file1.txt file2.txt
    

    Output:

    Overwrite Existing Files

  8. Delete specific files, such as file1.txt from the archive.zip

    console
    $ zip -d archive.zip file1.txt
    

    Output:

    Deleting Files

  9. Extract files from the archive.zip archive.

    console
    $ unzip archive.zip
    

    Output:

    Unzip Archive File

  10. 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:

    Best Compression Method

  11. Compress the file1.txt and file2.txt files and encrypt the archive.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:

    Encrypting Zip Archive

Use Advanced zip Command Options

  1. Use the -j option to compress the file1.txt and file2.txt files without including the directory paths in the archive.

    console
    $ zip -j archive.zip /path/to/file1.txt /path/to/file2.txt
    

    Output:

    Junk Directory Names

  2. Select and compress multiple *.txt files using the wildcard character *.

    console
    $ zip archive.zip *.txt
    

    Output:

    Using Wildcards

  3. Use the -v option to display detailed output when compressing the file1.txt and file2.txt files.

    console
    $ zip -v archive.zip file1.txt file2.txt
    

    Output:

    Verbose Output

  4. Compress the file1.txt and file2.txt files into archive.zip archive, encrypt the archive, and display detailed output.

    console
    $ zip -e -v archive.zip file1.txt file2.txt
    

    Output:

    Encrypt and Verbose

  5. Compress the file1.txt and file2.txt files into archive.zip while deleting the source files.

    console
    $ zip -m archive.zip file1.txt file2.txt
    

    Output:

    Archive Files and Delete Originals

  6. Unzip the archive.zip archive into the dir1 directory.

    console
    $ unzip archive.zip -d dir1/
    

    Output:

    Extract Zip File to Directory

  7. 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:

    Multiple Compression Levels

  8. Handle file conflicts in the archive.zip archive using the u 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:

    Handle File Conflicts

Combine zip with Other Commands

You can combine the zip command with other commands for advanced use cases as detailed below:

  1. Combine find and zip commands to find and compress files modified in the last 24 hours under the /home/vultr_user/dir directory. The zip archive.zip -@ inputs the file paths from the find command results and adds them to the archive.zip file.

    console
    $ find /home/vultr_user/dir -type f -mtime -1 -print | zip archive.zip -@
    

    Output:

    Find and Zip

  2. Combine find and zip 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:

    Size-based Compression

  3. Pipe the output of the ls -l /home/vultr_user/dir command to the zip command. The command lists all files in the /home/vultr_user/dir directory and compresses them into archive.zip. The hyphen - option allows the zip command to read from standard input.

    console
    $ ls -l /home/vultr_user/dir | zip archive.zip -
    

    Output:

    Stream and Zip

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.