Home > Archive/Compression > zip

zip Command Guide: Compressing Files and Directories

zip command is used to compress files and directories in .zip format on Linux and Unix-like operating systems. The .zip format is widely supported across various operating systems, including Windows, making it very convenient for file exchange and distribution. Through this guide, learn how to install the zip command, basic usage, and various compression options.

Installing zip

`zip` command may not be pre-installed on most Linux distributions. Here are the installation methods for major operating systems.

Installing zip on Linux

You can install zip using the package manager for each distribution.

Installing zip on Debian/Ubuntu

sudo apt update
sudo apt install zip unzip

Use the APT package manager to install `zip`.

Installing zip on Fedora/CentOS/RHEL

sudo yum install zip unzip  # CentOS/RHEL 7 and below
sudo dnf install zip unzip  # Fedora/CentOS/RHEL 8 and above

Use YUM or DNF package manager to install `zip`.

Overview of zip

`zip` command compresses files and directories in the file system into a single archive file (`.zip` file). This archive saves space and makes it easier to manage multiple files by bundling them into a single file. In particular, the `.zip` format has excellent compatibility across operating systems, making it very useful when transferring files to other systems.

Key Roles of zip

zip command is primarily used for the following purposes:

Main Use Cases

  • Compressing Files and Directories: Bundles and compresses multiple files or an entire directory into a single .zip file.
  • File Transfer and Distribution: Prepares files for email attachments or downloads by compressing website files, software packages, documents, etc.
  • Backup: Stores important data in .zip format for safekeeping.
  • Cross-Platform Compatibility: Easily extractable in various environments such as Windows, macOS, and Linux.

Key zip Command Options

`zip` command offers various options for compression methods, including/excluding files, setting passwords, etc., allowing for flexible compression tasks.

1. Basic Compression and Addition

2. Compression Level and Exclusion

3. Encryption and Others

Generated command:

Try combining the commands.

Description:

`zip` Executes the command.

Combine the above options to virtually execute commands with AI.

unzip Command (Extract)

To extract a `.zip` file compressed with `zip`, use the `unzip` command. `unzip` is often installed along with `zip`.

Extracting .zip File

unzip my_archive.zip

Extracts the `my_archive.zip` file in the current directory.

Extract to Specific Directory (-d)

unzip project.zip -d /tmp/extracted_project

Extracts the `project.zip` file to the `/tmp/extracted_project` directory.

Check Contents Before Extracting (-l)

unzip -l backup.zip

Checks the contents of the `backup.zip` file without actually extracting it, showing which files and directories are included.

Usage Examples

Learn how to efficiently compress and manage files and directories through various usage examples of the `zip` command.

Compress All Files in Current Directory

zip -r all_files.zip .

Compresses all files and subdirectories in the current directory into `all_files.zip`.

Compress Specific Files Only

zip my_docs.zip document.txt image.jpg notes.md

Compresses the three files `document.txt`, `image.jpg`, and `notes.md` into `my_docs.zip`.

Compress Directory with Best Compression

zip -r -9 code_backup.zip source_code/

Compresses the `source_code` directory into `code_backup.zip` at the highest compression ratio. Compression time may increase.

Exclude Specific Files While Compressing

zip -r project_release.zip my_project/ -x "*.log" -x "my_project/node_modules/*"

Compresses the `my_project` directory while excluding files with the `.log` extension and the `node_modules` directory.

Set Password for Compressed File

zip -r -e secure_archive.zip private_data/

Compresses the `private_data` directory into `secure_archive.zip` and sets a password. A prompt will appear asking for the password during execution.

Delete Original File After Compression (Use with Caution)

zip -m old_files.zip old_files.txt

Compresses the `old_files.txt` file to create `old_files.zip` and then deletes the original `old_files.txt`. (Use when moving files without backup)



Same category commands