Creating Zip Files in Linux

To create a zip file from any directory in Linux, you can use the zip command-line utility. Here’s a general guide with useful options:

Basic Installation

First, ensure the zip utility is installed:

sudo apt install zip

Basic Usage

The basic syntax for creating a zip file is:

zip -r archive_name.zip directory_to_compress

Where:

  • -r means recursive (include all subdirectories)
  • archive_name.zip is the name you want to give your zip file
  • directory_to_compress is the directory you want to zip

Useful Options

Compression Level

zip -r -9 archive_name.zip directory_to_compress

The -9 flag specifies maximum compression (default is -6).

Exclude Files

zip -r archive_name.zip directory_to_compress -x "*.git/*" "*.log" "*/node_modules/*"

The -x flag allows you to exclude files/directories using patterns.

Add Files to Existing Archive

zip -u archive_name.zip new_file.txt

The -u flag updates an existing zip with new or modified files.

Store Files Without Compression

zip -r -0 archive_name.zip directory_to_compress

The -0 flag stores files without compression (faster but larger files).

Verbose Output

zip -r -v archive_name.zip directory_to_compress

The -v flag shows detailed progress during compression.

Split Large Archives

zip -r -s 2g archive_name.zip directory_to_compress

The -s flag splits the archive into specified size chunks (2GB in this example).

Password Protection

zip -r -e archive_name.zip directory_to_compress

The -e flag encrypts the archive with a password (you’ll be prompted to enter it).

Quiet Operation

zip -r -q archive_name.zip directory_to_compress

The -q flag suppresses non-error messages.

Move Files to Archive (Delete After Zipping)

zip -r -m archive_name.zip directory_to_compress

The -m flag moves files to the archive (deletes originals after successful archiving).

Examples

Create a backup of your home directory:

zip -r ~/backup.zip ~ -x "*/.*" "*/node_modules/*" "*/venv/*"

Archive a web project excluding development files:

zip -r website.zip /var/www/mysite -x "*.git/*" "*.log" "*/node_modules/*" "*/tmp/*"