Zip and Unzip Files in Bash

Summary: Compress and extract files using zip, tar, gzip.


Working with compressed files is an everyday task for developers, sysadmins, and power users on Unix-like systems. Knowing how to zip and unzip files efficiently using Bash empowers you to save disk space, speed up file transfers, and automate backups. In this article, we’ll cover the most common tools and commands to compress and extract files: zip, unzip, tar, and gzip.


1. Using zip and unzip

Install the Tools

Most distributions don't install these by default. Use your package manager:

# Debian/Ubuntu
sudo apt install zip unzip

# Fedora/RHEL
sudo dnf install zip unzip

Compress Files with zip

To compress individual files or directories:

zip archive_name.zip file1.txt file2.jpg folder/
  • archive_name.zip: Name of the new archive.
  • You can zip several files and directories at once.

Example:

zip -r project.zip src/ README.md docs/
  • The -r (recursive) switch is needed to compress directories.

List Archive Contents

unzip -l archive_name.zip

Extract ZIP Files

To extract files from a zip archive:

unzip archive_name.zip

Extract to a specific directory:

unzip archive_name.zip -d /path/to/extract/

2. Compressing and Extracting with tar

The tar command is ubiquitous for creating .tar, .tar.gz, and .tar.bz2 archives.

Compress with tar

  • Create a .tar archive:

    tar cvf archive.tar file1 file2 dir/
    
    • c: create, v: verbose, f: file
  • Create a .tar.gz (gzip-compressed) archive:

    tar czvf archive.tar.gz file1 file2 dir/
    
    • z: gzip compression
  • Create a .tar.bz2 (bzip2-compressed) archive:

    tar cjvf archive.tar.bz2 file1 file2 dir/
    
    • j: bzip2 compression

Examples:

# Compress the 'myfolder' folder into a .tar.gz archive
tar czvf myfolder.tar.gz myfolder

List Archive Contents

tar tvf archive.tar.gz

Extract Files with tar

To extract all files:

tar xvf archive.tar

To extract gzip-compressed archives:

tar xzvf archive.tar.gz

To extract to a specific directory:

tar xzvf archive.tar.gz -C /path/to/extract/

Extracting a single file:

tar xzvf archive.tar.gz path/to/file.txt

3. Using gzip and gunzip

gzip is best for compressing individual files, not directories.

Compress a File

gzip filename.txt
# Creates 'filename.txt.gz' and removes the original.

Keep the original with -k:

gzip -k filename.txt

Decompress a Gzipped File

gunzip filename.txt.gz
# Restores 'filename.txt'

Or use gzip -d:

gzip -d filename.txt.gz

4. Quick Reference Table

Action Command Example
Zip files zip my.zip file1.txt folder/
Unzip file unzip my.zip
Tar and gzip folder tar czvf my.tar.gz folder/
Untar and ungzip tar xzvf my.tar.gz
Gzip compress gzip file.txt
Gzip decompress gunzip file.txt.gz

5. Automating with Bash Scripts

You can compress and extract files in automated scripts. Here’s an example script to back up a directory:

#!/bin/bash
SRC="/home/user/data"
DEST="/home/user/backups"
DATE=$(date +%F)
tar czvf "$DEST/data-$DATE.tar.gz" "$SRC"

Save as backup.sh, make executable: chmod +x backup.sh


Conclusion

Mastering file compression in Bash with zip, tar, and gzip is essential. Use zip for cross-platform ZIP files, tar.gz for efficient UNIX-style backups, and gzip for single-file compression. Integrate these commands into your workflows and scripts to optimize storage and streamline data transfer.