How to watch a directory for new file creation in Linux

December 13, 2023 / How-to Guide

This article explains how to monitor a directory for new file creation in Linux and automatically trigger actions when new files appear.

A common and lightweight way to achieve this is by using inotify-tools, which allows real-time monitoring of filesystem events.

Installing inotify-tools
First, install inotify-tools using your package manager.

Ubuntu / Debian-based systems

sudo apt update
sudo apt install inotify-tools

On other distributions, install the equivalent inotify-tools package using the appropriate package manager.

Understanding inotifywait Options

The examples below use the inotifywait command. Key options include:

  • -m (monitor): Keeps monitoring continuously instead of exiting after one event
  • -e create: Watches for file creation events
  • -r: Enables recursive monitoring of subdirectories
  • –format ‘%w%f’: Displays the full path of the created file

Creating a Shell Script to Monitor a Directory
Below is a practical example that monitors a directory and logs newly created files.

Example Script

#!/bin/bash
# Directory to monitor
MONITOR_DIR="/var/www/uploads"
# Log file
LOG_FILE="/var/log/file-monitor.log"
inotifywait -m -e create --format '%f' "$MONITOR_DIR" | while read FILE
do
    echo "$(date): New file created - $FILE" >> "$LOG_FILE"
done

Script Explanation

  • The script continuously monitors the specified directory
  • Each new file creation event is logged with a timestamp
  • You can replace the logging command with an API call, backup script, or notification action

Example Output:

Setting up watches.
Watches established.
/var/www/uploads/image1.jpg
/var/www/uploads/report.pdf

Each entry indicates a newly created file detected in real time.

Making the Script Executable and Running It

  1. Save the script (for example, monitor.sh).
  2. Make it executable:
    chmod +x monitor.sh
  3. Run the script:
    ./monitor.sh

Running the Script in the Background
To keep the script running after logging out, use nohup, screen, or tmux.
Example using nohup:

nohup ./monitor.sh &

Running the Script as a systemd Service

For long-term or production use, running the script as a systemd service is recommended.

Steps:

  1. Move the script to a suitable location:
    sudo mv monitor.sh /usr/local/bin/
    sudo chmod +x /usr/local/bin/monitor.sh
  2. Create a systemd service file:
    sudo nano /etc/systemd/system/monitor.service
  3. Add the following content:
    [Unit]
    Description=File Monitor Service
    
    [Service]
    ExecStart=/usr/local/bin/monitor.sh
    Restart=always
    User=nobody
    Group=nogroup
    
    [Install]
    WantedBy=multi-user.target
  4. Reload systemd and start the service:
    sudo systemctl daemon-reload
    sudo systemctl enable monitor.service
    sudo systemctl start monitor.service
  5. Verify service status:
    sudo systemctl status monitor.service

Recursive Directory Monitoring

By default, inotify does not monitor subdirectories. To enable recursive monitoring, add the -r flag:

inotifywait -m -r -e create "$MONITOR_DIR"

Limitations of inotify-based Monitoring

  • Does not monitor subdirectories unless -r is used
  • Limited by system watch limits (fs.inotify.max_user_watches)
  • Events may be missed during extremely high file-creation rates
  • Not ideal for very large directory trees or some network file systems

Alternatives to inotify-tools

Depending on your use case, consider these alternatives:

  • systemd Path Units
    Automatically trigger services when files change; more reliable for production systems
  • Auditd
    Useful for security auditing and compliance tracking
  • Custom daemons or applications
    Recommended for complex workflows or high-volume environments

Monitoring a directory for new file creation in Linux enables real-time automation and awareness. Using inotify-tools provides a simple and efficient solution, while systemd-based approaches may be better suited for long-running production environments.

By choosing the right method and understanding its limitations, you can reliably track file system changes and automate workflows effectively.

Spread the love