BASH

Monitor Directory for New Files and Process

Implement a bash script that continuously monitors a specified directory for new files, processes them, and moves them to an archived location.

#!/bin/bash

WATCH_DIR="/path/to/monitor"
PROCESS_DIR="/path/to/processed"
ARCHIVE_DIR="/path/to/archive"

# Create directories if they don't exist
mkdir -p "$WATCH_DIR"
mkdir -p "$PROCESS_DIR"
mkdir -p "$ARCHIVE_DIR"

LOG_FILE="/tmp/file_watcher.log"

echo "Monitoring $WATCH_DIR for new files... (Press Ctrl+C to stop)" | tee -a "$LOG_FILE"

while true; do
  # Find new files that are regular files and not directories
  find "$WATCH_DIR" -maxdepth 1 -type f -print0 | while IFS= read -r -d $'\0' NEW_FILE;
  do
    BASENAME=$(basename "$NEW_FILE")
    echo "[$(date)] Found new file: $NEW_FILE" | tee -a "$LOG_FILE"

    # Simulate processing
    echo "[$(date)] Processing $BASENAME..." | tee -a "$LOG_FILE"
    # Your processing logic here, e.g., 'cat "$NEW_FILE"', 'process_data "$NEW_FILE" > "$PROCESS_DIR/$BASENAME.out"'
    sleep 1 # Simulate work

    # Move processed file to archive
    mv "$NEW_FILE" "$ARCHIVE_DIR/$BASENAME"
    if [ $? -eq 0 ]; then
      echo "[$(date)] Moved $BASENAME to $ARCHIVE_DIR" | tee -a "$LOG_FILE"
    else
      echo "[$(date)] Error moving $BASENAME to archive." | tee -a "$LOG_FILE"
    fi
  done

  sleep 5 # Check every 5 seconds
done
How it works: This script continuously monitors a specified `WATCH_DIR` for new files. When a new file is detected, it simulates a 'processing' step (you'd replace this with your actual logic, like data parsing or transformation) and then moves the file to an `ARCHIVE_DIR`. It uses `find` with `-print0` and `read -r -d $'\0'` for robust handling of filenames with spaces or special characters, making it ideal for event-driven automation.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs