LinuxCommandLibrary

zip

Create compressed archive files

TLDR

Add files/directories to a specific archive

$ zip [[-r|--recurse-paths]] [path/to/compressed.zip] [path/to/file_or_directory1 path/to/file_or_directory2 ...]
copy

Remove files/directories from a specific archive
$ zip [[-d|--delete]] [path/to/compressed.zip] [path/to/file_or_directory1 path/to/file_or_directory2 ...]
copy

Archive files/directories excluding specified ones
$ zip [[-r|--recurse-paths]] [path/to/compressed.zip] [path/to/file_or_directory1 path/to/file_or_directory2 ...] [[-x|--exclude]] [path/to/excluded_files_or_directories]
copy

Archive files/directories with a specific compression level (0 - the lowest, 9 - the highest)
$ zip [[-r|--recurse-paths]] -[0..9] [path/to/compressed.zip] [path/to/file_or_directory1 path/to/file_or_directory2 ...]
copy

Create an encrypted archive with a specific password
$ zip [[-re|--recurse-paths --encrypt]] [path/to/compressed.zip] [path/to/file_or_directory1 path/to/file_or_directory2 ...]
copy

Archive files/directories to a multi-part split Zip archive (e.g. 3 GB parts)
$ zip [[-rs|--recurse-paths --split-size]] [3g] [path/to/compressed.zip] [path/to/file_or_directory1 path/to/file_or_directory2 ...]
copy

Print a specific archive contents
$ zip [[-sf|--split-size --freshen]] [path/to/compressed.zip]
copy

SYNOPSIS

zip [options] [zipfile.zip] [file ... | directory/]

PARAMETERS

-r
    Recursively archive directory contents.

-q
    Quiet mode: suppress output messages.

-v
    Verbose mode: show detailed progress.

-e
    Encrypt archive with password (prompts interactively).

-d
    Delete specified files from existing zipfile.

-u
    Update: add newer files to existing archive.

-f
    Freshen: update only existing files if newer.

-m
    Move files into archive (delete originals after).

-j
    Junk paths: store files without directory structure.

-0 to -9
    Compression level: -0 (none) to -9 (maximum).

-T
    Test archive integrity after creation.

-x pattern
    Exclude files matching shell glob patterns.

-i pattern
    Include only files matching patterns.

-s size
    Split into multi-part archives of given size (e.g., 1g).

-o
    Set archive timestamp to latest file's time.

-D
    Do not include directory entries.

-g
    Grow: append to existing archive.

-h
    Display help summary.

DESCRIPTION

The zip command-line utility creates ZIP archives, compressing one or more files or directories into a single portable file for backup, distribution, or storage. Developed by the Info-ZIP project, it implements the ubiquitous ZIP format using the DEFLATE compression algorithm by default, balancing speed and compression ratio effectively.

Key capabilities include recursive directory traversal (-r), password encryption (-e), splitting large archives into volumes (-s), and fine-grained control over what to include or exclude via patterns (-i, -x). It supports updating (-u), freshening (-f), or appending (-g) to existing archives without recreating them entirely. Compression levels range from -0 (store only) to -9 (maximum).

Zip is cross-platform, lightweight, and widely available on Linux, Unix, Windows, and macOS. It handles symbolic links, permissions, and timestamps, though Unicode filenames require explicit handling (-O utf8). Ideal for scripts and automation, it's often paired with unzip for extraction. Limitations include weak legacy encryption and potential issues with very long paths on FAT filesystems.

CAVEATS

Traditional encryption (-e) uses weak ZipCrypto; prefer AES via zip -X or modern tools like 7z. Filenames >255 chars or with colons may fail. Unicode requires -O utf8. Not suitable for streaming large data.

BASIC USAGE EXAMPLE

zip -r backup.zip /home/user/docs/
Recursively zips entire directory.

TEST ARCHIVE

zip -T archive.zip
Verifies zipfile without extracting.

EXCLUDE PATTERNS

zip -r data.zip . -x "*.tmp" "*.bak"
Archives current dir, skipping temp files.

HISTORY

Originating from the Info-ZIP open-source project in 1989, zip 1.0 emulated PKZIP for Unix. Maintained actively, version 3.0 (2008) added AES encryption and Unicode. Now at 3.0+, it's standard in most distros since the 1990s.

SEE ALSO

unzip(1), zipinfo(1), zipcloak(1), zipnote(1), zipsplit(1), tar(1), gzip(1), 7z(1)

Copied to clipboard