ZIP Command Reference Examples
We show many examples of using zip to store and retrieve files!
We put get Grok to write some detailed command examples. Everyone just assumed zip was the best - and we were surprised to find out many formats exceed it!
- Sometimes we never think of this stuff. Let the AI come up with cool commands we might not have thought of!
Guide to Using the zip Command for File Compression
Alternatives with Higher Compression Ratios
The zip utility relies on the DEFLATE algorithm, which offers acceptable compression for general use but is frequently surpassed in compression ratio by more advanced tools. The following utilities typically achieve superior results (often 20–70% smaller archives, depending on data type, at the expense of higher CPU or memory usage):
- 7-Zip (
7zcommand, LZMA2 algorithm): Provides significantly higher compression ratios than ZIP for most file types, with broad format support. - XZ Utils (
xzcommand, LZMA2 algorithm): Delivers excellent ratios for large or repetitive data, especially text and binaries. - WinRAR (
rarcommand, RAR format): Consistently outperforms ZIP in compression efficiency. - bzip2: Utilizes the Burrows-Wheeler transform and yields better ratios than DEFLATE for textual content.
- Zstandard (
zstdcommand): Offers configurable high ratios with strong performance characteristics. - lrzip: Specialized for long-range redundancy (e.g., backups), providing extreme compression on suitable datasets.
These tools are recommended when maximum size reduction is prioritized over universal compatibility.
25 Examples of Using zip to Compress Files
The examples below demonstrate practical applications of the zip command. The first 20 focus exclusively on compression operations. Examples 21–25 incorporate rsync for secure, efficient transfer of the resulting archives to remote systems. Each command is presented in its own code block, followed by a precise explanation of the parameters and their rationale.
Example 1: Compressing a single file
zip archive.zip document.pdf
This command creates (or updates) archive.zip and adds the specified file. No special parameters are used, invoking the default compression level of 6. It is appropriate for simple, one-off archiving of individual files where standard efficiency suffices.
Example 2: Compressing multiple files
zip archive.zip file1.txt file2.txt file3.log
The command adds the listed files to archive.zip. Files are enumerated explicitly on the command line. This approach is efficient for grouping a small, known set of files without recursion.
Example 3: Recursive compression of a directory
zip -r archive.zip /path/to/directory/
The -r (recursive) parameter traverses the directory and all subdirectories, ensuring every contained file and subdirectory is included in the archive. This is essential for complete directory backups.
Example 4: Recursive compression with maximum compression level
zip -9 -r archive.zip /path/to/directory/
The -9 parameter selects the highest compression level (range 0–9). Combined with -r, it minimizes the final archive size at the cost of longer processing time, ideal when storage space is the primary constraint.
Example 5: Recursive compression with fastest compression level
zip -1 -r archive.zip /path/to/directory/
The -1 parameter applies the fastest (lowest) compression level. With -r, it prioritizes speed over size reduction, suitable for time-sensitive operations or when subsequent compression is expected.
Example 6: Storing files without compression
zip -0 -r archive.zip /path/to/directory/
The -0 parameter disables compression entirely (store mode). Paired with -r, it simply bundles files into the ZIP container, preserving original sizes for scenarios requiring rapid archiving or compatibility with non-compressing workflows.
Example 7: Encrypting an archive with a password
zip -e -r secure.zip /path/to/directory/
The -e (encrypt) parameter enables AES-based encryption and prompts interactively for a password. Combined with -r, it produces a password-protected recursive archive, ensuring confidentiality during storage or transmission.
Example 8: Updating an existing archive with new or changed files
zip -u archive.zip /path/to/newfiles/*
The -u (update) parameter adds new files and replaces existing ones only if they are newer. This maintains an incremental archive without re-compressing unchanged content, conserving time and resources.
Example 9: Freshening files in an existing archive
zip -f archive.zip
The -f (freshen) parameter updates files already present in the archive if newer versions exist on disk, without adding new files. It is useful for synchronizing an archive with minimal changes.
Example 10: Moving files into an archive (delete originals after successful addition)
zip -m archive.zip /path/to/files/*
The -m (move) parameter deletes the original files after they are successfully added to the archive. This conserves disk space when the originals are no longer required post-compression.
Example 11: Recursive compression while excluding specific patterns
zip -r -x "*.tmp" "*.log" archive.zip /path/to/directory/
The -r parameter enables recursion; -x excludes files matching the listed patterns. This selectively omits temporary or log files, producing a cleaner, smaller archive.
Example 12: Recursive compression including only specific file types
zip -r -i "*.jpg" "*.png" archive.zip /path/to/directory/
The -r parameter recurses through the directory; -i restricts inclusion to files matching the patterns. This creates an archive containing only the designated image files, ignoring all others.
Example 13: Splitting an archive into multiple volumes
zip -r -s 50m multi.zip /path/to/directory/
The -r parameter enables recursion; -s 50m splits the output into volumes no larger than 50 MB each (suffixes k, m, g supported). This facilitates distribution of large archives across media with size limits.
Example 14: Recursive compression in quiet mode
zip -q -r archive.zip /path/to/directory/
The -q (quiet) parameter suppresses all non-error output. With -r, it performs the operation silently, which is preferable in scripts or automated environments.
Example 15: Recursive compression with verbose output
zip -v -r archive.zip /path/to/directory/
The -v (verbose) parameter displays detailed progress and diagnostic information. Combined with -r, it aids troubleshooting during complex archiving tasks.
Example 16: Testing the integrity of an existing archive
zip -T archive.zip
The -T (test) parameter verifies the archive’s CRC checksums and structure without extracting files. It confirms data integrity before distribution or long-term storage.
Example 17: Deleting files from an existing archive
zip -d archive.zip "old/*.bak"
The -d (delete) parameter removes entries matching the pattern from the archive. This maintains a lean archive by eliminating obsolete files without recreating it.
Example 18: Recursive compression preserving symbolic links
zip -y -r archive.zip /path/to/directory/
The -y parameter stores symbolic links as links rather than dereferencing them. With -r, it accurately preserves the original filesystem structure in the archive.
Example 19: Creating an archive from a list of files in a text file
zip -@ archive.zip < filelist.txt
The -@ parameter reads filenames (one per line) from standard input instead of the command line. This enables efficient archiving of large or dynamically generated file lists.
Example 20: Creating an archive using output from the find command
find /path/to/directory -type f -name "*.txt" -print | zip -@ texts.zip
The find command generates the list of matching files; -@ directs zip to read that list from standard input. This constructs a targeted archive without manual enumeration.
Example 21: Recursive compression at maximum level followed by rsync transfer
zip -9 -r backup.zip /path/to/directory/ && rsync -avz backup.zip user@remotehost:/backups/
The zip portion uses -9 for maximum compression and -r for recursion. The chained rsync employs -a (archive mode, preserving permissions and timestamps), -v (verbose), and -z (compresses data in transit using zlib). This workflow creates a compact archive and transfers it efficiently to a remote host.
Example 22: Rsync transfer of a pre-compressed archive with progress and resume support
rsync -avzP --partial backup.zip user@remotehost:/backups/
(Assumes backup.zip was previously created with zip.) The parameters include -a (archive mode), -v (verbose), -z (compression), -P (progress and partial/resume capability), and --partial (retains incomplete files for resumption). This ensures reliable transfer of ZIP archives over unreliable networks.
Example 23: Rsync a directory to a remote host, then compress remotely
rsync -avz /local/directory/ user@remotehost:/remote/directory/ && ssh user@remotehost "zip -9 -r /remote/backup.zip /remote/directory/"
The rsync step uses -a (archive), -v (verbose), and -z (compress transfer). The subsequent ssh executes zip with -9 (maximum compression) and -r (recursive) on the remote side. This defers compression to the destination, minimizing local resource usage.
Example 24: Compressing multiple directories into separate archives, then rsyncing them
for d in dir1 dir2 dir3; do zip -9 -r "${d}.zip" "$d/"; done && rsync -avz *.zip user@remotehost:/backups/
Each iteration of the loop applies zip with -9 (maximum compression) and -r (recursive). The rsync command then transfers all resulting archives using -a (archive), -v (verbose), and -z (compression). This automates batch compression and centralized backup.
Example 25: Compressing a directory with move semantics, then rsyncing and cleaning up locally
zip -9 -m -r temp.zip /path/to/directory/ && rsync -avzP temp.zip user@remotehost:/backups/ && rm temp.zip
The zip command uses -9 (maximum compression), -m (move/delete originals after addition), and -r (recursive). The rsync employs -a (archive), -v (verbose), -z (compression), and -P (progress/resume). The final rm removes the temporary archive after successful transfer, ensuring no local disk residue remains.
These examples provide a comprehensive reference for effective use of zip in both standalone and networked environments. Adjust paths, filenames, and remote credentials as required for your specific infrastructure.