Using the zip Command in Linux to Compress Files and Directories

The zip command in Linux is a versatile tool designed to compress both files and directories into a unified .zip archive. This compression helps reduce file size and conveniently bundles multiple items for easier storage or faster data transmission. The .zip format is widely supported across different operating systems including Windows, macOS, Linux, and Unix, making it ideal for cross-platform file handling.

This guide covers how to leverage the zip command in Linux to efficiently compress and manage your files.

Linux zip Command: Syntax Overview

Here’s the general syntax used for the zip command:

zip [options] zipfile files

Explanation of components:

  • [options]: Optional flags that adjust how the command functions.
  • zipfile: The name you assign to the output archive.
  • files: The specific files or directories you want to compress.

Creating Test Files and Directories

Follow these steps to generate sample files and directories to test out the zip command on your Linux system:

  1. Navigate to your user’s home directory:
  1. Create several text files like file.txt, file1.txt, file2.txt, newfile.txt, and file.log:

$ touch file.txt file1.txt file2.txt newfile.txt file.log

  1. Generate directories named dir and dir1. Inside dir1, add a sub-directory called sub_dir:

$ mkdir -p dir dir1/sub_dir

  1. Change into the dir directory:
  1. Inside the dir folder, create files like file1.txt, file2.txt, sys.log, and auth.log:

$ touch file1.txt file2.txt sys.log auth.log

Frequently Used zip Command Flags

Below is a list of commonly used zip command options and their functions:

Option Description
-r Recursively compress directories and all their contents.
-j Exclude directory names from the archive; only include files.
-m Move files into the archive, deleting originals after compression.
-u Update archive contents with newer versions of files.
-d Remove specific entries from the archive.
-x Exclude files or patterns you don’t want to compress.
-q Enable quiet mode to suppress command output.
-v Activate verbose mode to show detailed process information.
-9 Apply the highest level of compression for minimal archive size.
-e Encrypt the archive with a password for extra security.

Run Practical zip Command in Linux with Examples

Follow these instructions to apply real-world examples of the zip command in Linux:

Compressing a Single File

Package a file named file.txt into a zip archive called archive.zip:

$ zip archive.zip file.txt

Compressing Multiple Files

Combine file.txt, file1.txt, and file2.txt into one archive:

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

Compressing a Directory

Zip the entire dir directory, including its subfolders and files:

Excluding Specific Files

Create an archive without including files that have the .log extension:

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

Appending Files to an Archive

Include newfile.txt into the existing archive.zip file:

$ zip -u archive.zip newfile.txt

Listing Archive Contents

Display the internal file list of archive.zip using the -sf option:

Overwriting or Updating Files

Refresh file1.txt and file2.txt in the archive if newer versions are available:

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

Removing Files from Archive

Erase file1.txt from the archive.zip file:

$ zip -d archive.zip file1.txt

Unzipping Archive

Extract all files stored in archive.zip to the current directory:

Applying Best Compression

Use maximum compression on file1.txt and file2.txt:

$ zip -9 archive.zip file1.txt file2.txt

Encrypting the Archive

Create a password-protected zip archive for file1.txt and file2.txt:

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

Use Advanced zip Command Options

Excluding Directory Structure

Compress files without retaining their original folder paths:

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

Wildcard Compression

Compress all .txt files using the wildcard symbol:

Verbose Mode

Enable verbose output while compressing files:

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

Encrypt and Verbose Output

Encrypt and display compression details simultaneously:

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

Archive and Delete Originals

Compress files and remove the source versions post-archiving:

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

Extract to Target Directory

Unzip archive.zip contents into the dir1 directory:

$ unzip archive.zip -d dir1/

Use Multiple Compression Levels

Create an archive using recursive and highest compression together:

$ zip -r -9 archive.zip dir/

Managing Archive Conflicts

Update only .txt files in the archive if they are newer, using the -n flag:

$ zip -u archive.zip *.txt -n .txt

Combine zip with Other Commands

You can integrate the zip command with other Linux commands to address more advanced scenarios. Below are several examples:

Compress Recently Modified Files

Use the find and zip commands together to identify and compress files that have been updated within the past 24 hours in the /path/to/your/files directory. The -@ option reads file paths from the output of find and includes them in the archive.zip file:

$ find /path/to/your/files -type f -mtime -1 -print | zip archive.zip -@

Compress Files Based on Size

Locate and compress files that are larger than 10MB using find and zip. The resulting archive is named large_files.zip:

$ find /path/to/your/files -type f -size +10M -print | zip large_files.zip -@

Stream Output into zip

Combine the output of ls -l /path/to/your/files with zip. This approach lists the directory contents and compresses the result into archive.zip using the hyphen - to read from standard input:

$ ls -l /path/to/your/files | zip archive.zip -

Conclusion

In this tutorial, you learned how to use the zip command on Linux to compress files and directories. This tool helps minimize disk space usage, accelerates file transfers, and simplifies file sharing. To explore additional options and configuration settings, run man zip to open the manual page.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: