Compressing Log Files with gzip and zip in Bash

Application log files compressed into individual and portable archive bundles

What You’ll Learn

In this lesson, you will learn how to reduce the size of application log files from Bash. You will use gzip for compressing one file and zip for collecting one or more files into a portable archive.

  • Compress and decompress a single log file with gzip.
  • Create and extract a ZIP archive with zip and unzip.
  • Choose between gzip and zip for common log-management tasks.
Ad

The Concept

Compression reduces the amount of disk space a file uses. This is useful for application logs because logs can grow quickly, especially when you store them for troubleshooting, compliance, or transfer to another system.

gzip compresses one file at a time. By default, a command such as gzip application.log creates application.log.gz and removes the uncompressed file. The .gz format is common on Linux and Unix systems.

zip creates an archive that can contain one or more files. It is useful when you want to package several related logs together. The matching unzip command extracts ZIP archives.

As a simple rule:

  • Use gzip when you are compressing one log file and want a straightforward Linux-friendly format.
  • Use zip when you need to bundle multiple files into one archive that is widely supported across operating systems.

If you need to combine many files while using gzip compression, a common Linux approach is tar together with gzip. You can learn more about creating and extracting compressed archives with tar.

Basic Example

The following example creates a small application log, makes a gzip copy without deleting the original, creates a ZIP archive, and then demonstrates how to read the compressed data.

mkdir -p log-demo

printf '%s\n' \
    '2026-08-18 10:14:22 INFO  Server started' \
    '2026-08-18 10:15:07 WARN  Database response was slow' \
    '2026-08-18 10:16:31 ERROR Payment service returned HTTP 503' \
    > log-demo/application.log

gzip -c log-demo/application.log > log-demo/application.log.gz
zip -q log-demo/application-logs.zip log-demo/application.log

gzip -dc log-demo/application.log.gz > log-demo/restored-from-gzip.log
unzip -p log-demo/application-logs.zip log-demo/application.log \
    > log-demo/restored-from-zip.log

ls -lh log-demo

Expected Output

The exact file sizes depend on the gzip and ZIP versions on your system. The directory should contain the original log, both compressed files, and two restored copies.

application-logs.zip
application.log
application.log.gz
restored-from-gzip.log
restored-from-zip.log

How the Code Works

A comparison flow begins with application logs and a format choice. The gzip branch compresses a single log into a .gz file and restores it with gzip decompression. The zip branch bundles multiple logs into a .zip archive and inspects or extracts it with unzip.
Use gzip for a single Linux-friendly log file; use zip to bundle multiple logs into a portable archive.
  • mkdir -p log-demo creates a directory for the practice files. The -p option avoids an error if the directory already exists.
  • The printf command writes sample log lines into log-demo/application.log. The > operator sends the output into the file.
  • gzip -c writes compressed data to standard output instead of replacing the input file. The redirection operator saves that compressed data as application.log.gz. This lets the original log remain available.
  • zip -q creates application-logs.zip and adds the application log to it. The -q option means “quiet,” so ZIP does not print a line for every file it adds.
  • gzip -dc decompresses the gzip file and writes the original text to standard output. The -d means decompress, and -c means write the result to standard output.
  • unzip -p prints a file from a ZIP archive without extracting it to the current directory. The command redirects that printed content into restored-from-zip.log.
  • ls -lh lists the results. The -h option displays file sizes in a human-readable format.

For a normal gzip workflow, you can also use gunzip application.log.gz to decompress a file and restore the original filename. Unlike the -c approach above, this normally removes the compressed file after successful decompression.

Another Example

Suppose an application creates a current log and a rotated log from the previous day. You can keep the current log available for the application while creating a compressed copy of it, then package both original logs into a single ZIP file for transfer.

mkdir -p rotated-logs

printf '%s\n' \
    '2026-08-19 08:00:01 INFO  Worker queue started' \
    '2026-08-19 08:03:44 INFO  Processed 250 requests' \
    > rotated-logs/current.log

printf '%s\n' \
    '2026-08-18 23:58:12 INFO  Daily processing completed' \
    '2026-08-18 23:59:03 INFO  Worker queue stopped' \
    > rotated-logs/previous.log

gzip -c rotated-logs/current.log > rotated-logs/current.log.gz
zip -q rotated-logs/rotated-logs.zip \
    rotated-logs/current.log rotated-logs/previous.log

unzip -l rotated-logs/rotated-logs.zip

The gzip file is a compressed copy of only the current log. The ZIP file packages both uncompressed log files into one transferable archive. The unzip -l command lists the archive contents without extracting them.

When writing a script that performs log compression automatically, validate that the required files and commands exist before continuing. Bash strict mode can help scripts fail clearly instead of silently producing incomplete archives; see Bash strict mode for a safer scripting approach.

Common Mistakes

  • Expecting gzip to combine several files: gzip compresses individual files. To create one gzip-compressed package from several files, use an archive tool such as tar with gzip compression, or use zip.
  • Accidentally deleting the original: gzip application.log normally replaces the original with application.log.gz. Use gzip -c application.log > application.log.gz when you want to keep the original.
  • Trying to use unzip on a gzip file: A file ending in .gz should be handled by gunzip or gzip -d. A file ending in .zip should be handled by unzip.
  • Overwriting an existing output file: Redirection with > replaces an existing destination file. Check the destination name before running a command if the existing file matters.
  • Assuming every system has zip installed: gzip is commonly present on Linux systems, but zip and unzip may need to be installed separately.

Try It Yourself

Create a file named service.log containing two or three realistic log lines. Then create service.log.gz while keeping service.log. Finally, use gzip -dc to restore the compressed contents into a file named service-restored.log.

Compare the original and restored files with diff. If the command produces no output, the files have the same contents.

Challenge

Create a directory named practice-logs containing current.log and previous.log. Your solution should:

  • Keep current.log unchanged while creating current.log.gz.
  • Create weekly-logs.zip containing both original log files.
  • List the contents of the ZIP archive without extracting it.

Use printf to create a few sample log lines so the challenge is self-contained.

Solution

mkdir -p practice-logs

printf '%s\n' \
    '2026-08-20 09:00:00 INFO  API service started' \
    '2026-08-20 09:04:18 INFO  Health check passed' \
    > practice-logs/current.log

printf '%s\n' \
    '2026-08-19 17:45:10 INFO  API service stopped' \
    '2026-08-19 17:45:12 INFO  Log rotation completed' \
    > practice-logs/previous.log

gzip -c practice-logs/current.log > practice-logs/current.log.gz
zip -q practice-logs/weekly-logs.zip \
    practice-logs/current.log practice-logs/previous.log

unzip -l practice-logs/weekly-logs.zip

The gzip -c command creates a compressed copy and preserves current.log. The zip command adds both original logs to one archive, and unzip -l verifies the archive contents without extracting the files.

Key Takeaways

  • gzip compresses individual files and commonly creates files ending in .gz.
  • Use gzip -c when you want to keep the original log while creating a compressed copy.
  • zip packages one or more files into a single archive, and unzip extracts or inspects that archive.
  • Choose gzip for simple single-file compression and ZIP when bundling files for convenient transfer.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Ad
Ad
Ad