Operations 7 min read

Automatically Compress New Files with inotifywatch & gzip on Linux

This guide shows how to install inotify-tools and gzip, use inotifywatch to monitor directory events, filter specific events, and create a bash script with inotifywait that automatically moves and compresses newly added files for secure storage.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Automatically Compress New Files with inotifywatch & gzip on Linux

Installation

First install the required utilities on Debian‑based systems: sudo apt-get install inotify-tools gzip On other Linux distributions use the appropriate package manager.

Exploring inotifywatch

Create a test directory and start monitoring it:

mkdir incoming
inotifywatch -v incoming

In a second terminal, create a file:

cd incoming
touch newfile

Press Ctrl+C in the first terminal to stop monitoring. The output shows events such as CREATE, OPEN, WRITE, and CLOSE triggered by the touch command.

Filtering Specific Events

If only the create and moved_to events are needed, run: inotifywatch -v -e create -e moved_to incoming Then generate those events by creating a file and moving another file into the directory:

touch incoming/created
echo Testing123 >> incoming/created
touch /tmp/created2
mv /tmp/created2 incoming/

The resulting output lists only the two selected events, confirming that other events are ignored.

Automating with inotifywait

Create a destination directory for processed files: mkdir processed Write a script watch-incoming.sh that watches for the two events, moves the new file to processed, and compresses it with gzip:

#!/bin/bash
TARGET=~/incoming/
PROCESSED=~/processed/

inotifywait -m -e create -e moved_to --format "%f" $TARGET \
    | while read FILENAME
    do
        echo Detected $FILENAME, moving and zipping
        mv "$TARGET/$FILENAME" "$PROCESSED/$FILENAME"
        gzip "$PROCESSED/$FILENAME"
    done

The -m option makes inotifywait run continuously, and the -e options restrict monitoring to the desired events. The script receives each filename, moves it, and compresses it.

Make the script executable and start it:

chmod u+x watch-incoming.sh
./watch-incoming.sh

Now, creating a new file in incoming causes the script to move the file to processed and gzip it automatically. This technique can be extended to add watermarks, transcode videos, batch‑rename files, or any other custom processing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationGzipBashinotify
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.