DEV Community

Cover image for Zipping and Unzipping Files in Linux
Sanskriti Harmukh for Vultr

Posted on with Aashish Chaurasiya Originally published at docs.vultr.com

Zipping and Unzipping Files in Linux

zip and unzip are command-line utilities for compressing and extracting files and directories on Linux systems. zip combines multiple files and directories into a single compressed archive to save storage space and simplify file management, while unzip extracts the archived contents when needed. This guide covers installing both utilities and using them to compress individual files, archive entire directories, and extract archive contents. By the end, you'll have zip and unzip installed and working archive and extraction workflows for both individual files and whole directory trees.


Prerequisites

  • Access to a Linux server as a non-root sudo user

Install Zip and Unzip

The zip and unzip utilities are available through the default package managers on most Linux distributions.

Ubuntu & Debian:

1. Update the apt package index:

$ sudo apt update
Enter fullscreen mode Exit fullscreen mode

2. Install zip and unzip:

$ sudo apt install -y zip unzip
Enter fullscreen mode Exit fullscreen mode

CentOS/AlmaLinux/Rocky Linux/Fedora:

$ sudo dnf install -y zip unzip
Enter fullscreen mode Exit fullscreen mode

If you're using an older version of Rocky Linux, AlmaLinux, or CentOS that doesn't support dnf, replace dnf with yum in the command above.

Arch Linux:

$ sudo pacman -S zip unzip
Enter fullscreen mode Exit fullscreen mode

openSUSE:

$ sudo zypper install -y zip unzip
Enter fullscreen mode Exit fullscreen mode

Alpine Linux:

1. Update the apk package index:

$ sudo apk update
Enter fullscreen mode Exit fullscreen mode

2. Install zip and unzip:

$ sudo apk add zip unzip
Enter fullscreen mode Exit fullscreen mode

zip Command Syntax

$ zip [OPTIONS]... [COMPRESSED_FILENAME] [FILES/DIRS TO COMPRESS]...
Enter fullscreen mode Exit fullscreen mode

Where:

  • [OPTIONS] modify the command behavior and are optional
  • [COMPRESSED_FILENAME] is the name of the archive file the command creates
  • [FILES/DIRS TO COMPRESS] is the list of files or directories to archive

zip Command Usage

1. Compress a single file into a .zip archive:

$ zip archive-name.zip file1.txt
Enter fullscreen mode Exit fullscreen mode

2. Add multiple files to a new or existing archive:

$ zip archive-name.zip file1.txt file2.txt file3.txt
Enter fullscreen mode Exit fullscreen mode

If the archive doesn't exist, this creates a new one; if it exists, it updates the archive with the specified files.

3. Set the compression level (-0 for fastest/no compression, -9 for maximum compression):

$ zip -9 archive-name.zip file.txt
Enter fullscreen mode Exit fullscreen mode

4. Password-protect an archive with the -P option:

$ zip -P your_password archive-name.zip file4.txt
Enter fullscreen mode Exit fullscreen mode

5. Exclude specific files from the archive with the -x flag:

$ zip archive-name.zip *.txt -x secret.txt
Enter fullscreen mode Exit fullscreen mode

This adds all .txt files in the directory to the archive except secret.txt.

6. Update files in the archive only if the source file is newer, with the -u flag:

$ zip -u archive-name.zip updatedfile.txt
Enter fullscreen mode Exit fullscreen mode

7. View detailed output while compressing with the -v flag:

$ zip -v archive-name.zip file1.txt
Enter fullscreen mode Exit fullscreen mode

unzip Command Syntax

$ unzip [OPTIONS]... [ZIP_FILENAME] [MEMBER FILES/DIRS]...
Enter fullscreen mode Exit fullscreen mode

Where:

  • [OPTIONS] modify how the contents are extracted and are optional
  • [ZIP_FILENAME] is the .zip file to extract entirely or partially
  • [MEMBER FILES/DIRS] are files or directories within the archive to extract; if omitted, the command extracts everything

unzip Command Usage

1. Extract all files from an archive into the working directory:

$ unzip archive-name.zip
Enter fullscreen mode Exit fullscreen mode

2. Extract a single file instead of the full archive:

$ unzip archive-name.zip file1.txt
Enter fullscreen mode Exit fullscreen mode

3. Extract to a specific destination with the -d flag:

$ unzip archive-name.zip -d /path/to/destination
Enter fullscreen mode Exit fullscreen mode

4. Preview the files inside an archive without extracting with the -l flag:

$ unzip -l archive-name.zip
Enter fullscreen mode Exit fullscreen mode

5. Extract multiple specific files by listing them after the archive name:

$ unzip archive-name.zip file1.txt updatedfile.txt
Enter fullscreen mode Exit fullscreen mode

6. Overwrite existing files automatically with the -o flag:

$ unzip -o archive-name.zip
Enter fullscreen mode Exit fullscreen mode

7. Skip files that already exist in the destination with the -n flag:

$ unzip -n archive-name.zip
Enter fullscreen mode Exit fullscreen mode

8. View detailed output during extraction with the -v flag:

$ unzip -v archive-name.zip
Enter fullscreen mode Exit fullscreen mode

Zip a Directory

1. Compress a directory and all its contents with the -r flag:

$ zip -r archive-name.zip directory-name
Enter fullscreen mode Exit fullscreen mode

2. Exclude specific files, file types, or folders during compression with the -x flag:

$ zip -r archive-name.zip directory-name -x "*.log" "*.tmp" "cache/*"
Enter fullscreen mode Exit fullscreen mode

3. Combine multiple directories into a single zip file:

$ zip -r archive-name.zip dir1 dir2 dir3
Enter fullscreen mode Exit fullscreen mode

4. Compress the entire working directory using a single dot:

$ zip -r archive-name.zip .
Enter fullscreen mode Exit fullscreen mode

Unzip a Directory

1. Extract a full directory structure into the working directory:

$ unzip archive-name.zip
Enter fullscreen mode Exit fullscreen mode

2. Extract into a specific location with the -d flag:

$ unzip archive-name.zip -d /path/to/destination
Enter fullscreen mode Exit fullscreen mode

3. Automatically overwrite existing files and directories with the -o flag:

$ unzip -o archive-name.zip -d /path/to/destination
Enter fullscreen mode Exit fullscreen mode

4. Skip overwriting existing files or directories with the -n flag:

$ unzip -n archive-name.zip -d /path/to/destination
Enter fullscreen mode Exit fullscreen mode

Next Steps

  • Automate periodic backups by scripting zip -r against a cron schedule
  • Combine zip with scp or rsync to transfer compressed archives between servers
  • Explore tar for archiving on systems where .tar.gz is the preferred format
  • Add password protection to any archive that contains sensitive files before sharing it

For the full guide with additional tips, visit the original article on Vultr Docs.

Top comments (0)