By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
IT Infras HolicIT Infras HolicIT Infras Holic
  • News
  • Linux
    • Application
    • SELinux
    • Centos
    • Ubuntu
  • Docker
  • Web Server
    • Kong
    • Nginx
    • Openlitespeed
  • Database
  • Mikrotik
  • Windows
  • Mail
  • Tools
    • 2048
    • Fantasy Forest
    • Hextris
    • Crossword
Search
  • Privacy Policy
© 2024. All Rights Reserved.
Font ResizerAa
IT Infras HolicIT Infras Holic
Font ResizerAa
  • News
  • Linux
  • Docker
  • Web Server
  • Database
  • Mikrotik
  • Windows
  • Mail
  • Tools
Search
  • News
  • Linux
    • Application
    • SELinux
    • Centos
    • Ubuntu
  • Docker
  • Web Server
    • Kong
    • Nginx
    • Openlitespeed
  • Database
  • Mikrotik
  • Windows
  • Mail
  • Tools
    • 2048
    • Fantasy Forest
    • Hextris
    • Crossword
Follow US
© 2022 Foxiz News Network. Ruby Design Company. All Rights Reserved.
IT Infras Holic > Blog > Linux > Application > Monitoring File & Directory Changes using Bash Script
LinuxApplication

Monitoring File & Directory Changes using Bash Script

writer
Share
3 Min Read
SHARE

In a world where automation is key and staying informed in real time can make all the difference, monitoring file changes on your server becomes more than just a convenience — it's a necessity.

A few weeks ago, one of my clients had a security incident: their website was compromised, and malicious files were silently uploaded to their server. It wasn’t until much later that the intrusion was discovered — long after suspicious scripts had been running undetected. This incident became a turning point, reminding me how crucial it is to have visibility over what’s happening inside the filesystem, especially in production environments.

That experience inspired me to create a simple yet powerful monitoring script. The goal was clear: track every change in specific directories, ignore irrelevant paths (like logs or temporary files), and send real-time alerts through Telegram whenever something changes — whether it's a new file, a modification, or a deletion.

In this post, I’ll walk you through the script I ended up with. It’s lightweight, bash-based, and leverages inotifywait to efficiently monitor multiple directories while excluding specific ones. If you’re running a CentOS or similar Linux VM and want quick, actionable visibility into file activity — this one's for you.

Let’s dive in.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash
 
BOT_TOKEN="YOUR_BOT_TOKEN"
CHAT_ID="YOUR_CHAT_ID"
 
# Included folders
INCLUDE_DIRS=(
  "/mnt/data/project1"
  "/mnt/data/project2"
)
 
# Excluded path patterns
EXCLUDE_PATTERNS=(
  "/tmp/"
  "/logs/"
)
 
send_telegram() {
    MESSAGE="$1"
    echo "Sending to Telegram: $MESSAGE"  # Debug
    curl -s -X POST "https://api.telegram.org/bot${BOT_TOKEN}/sendMessage" \
        -d chat_id="${CHAT_ID}" \
        -d text="$MESSAGE" >> /tmp/telegram_log.txt 2>&1
}
 
# Start monitoring all included folders in a single loop
inotifywait -m -r -e create -e modify -e delete -e move --format '%w%f %e' "${INCLUDE_DIRS[@]}" |
while read FILE EVENT; do
    # Skip excluded patterns
    skip=0
    for pattern in "${EXCLUDE_PATTERNS[@]}"; do
        if [[ "$FILE" == *"$pattern"* ]]; then
            skip=1
            break
        fi
    done
 
    [[ $skip -eq 1 ]] && continue
 
    echo "Detected: $FILE [$EVENT]"  # Debug
    send_telegram "📂 Change detected: $FILE [$EVENT]"
done

 

Parameters should you changes:

BOT_TOKEN

CHAT_ID

INCLUDE_DIRS

EXCLUDE_PATTERNS

You Might Also Like

Vulnerability Checker CVE-2024-3094

Unleashing the Potential of Knowledge Management with Wiki.js

How to Install Apache Guacamole with Docker Compose

Unlocking Seamless Remote Access: Exploring the Power of Apache Guacamole

Install Cyberpanel on Ubuntu 22.04

TAGGED: script, shell
Share This Article
Facebook Twitter Whatsapp Whatsapp LinkedIn Telegram Copy Link
Previous Article vulnerability Vulnerability Checker CVE-2024-3094
Leave a comment

Leave a Reply Cancel reply

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

Latest News

vulnerability
Vulnerability Checker CVE-2024-3094
Linux
wiki.js
Unleashing the Potential of Knowledge Management with Wiki.js
Application Linux
Install Apache Guacamole
How to Install Apache Guacamole with Docker Compose
Application Linux
Apache Guacamole
Unlocking Seamless Remote Access: Exploring the Power of Apache Guacamole
Application Linux

You Might also Like

How to map SFTP as a drive on Windows 10
LinuxWindows

How to map SFTP as a drive on Windows 10

3 Min Read
How to set up a VPN Site to Site (VPN S2S) between StrongSwan and Cloud VPN
Google Cloud Platform (GCP)Cloud ServicesUbuntu

How to set up a VPN Site to Site (VPN S2S) between StrongSwan and Google Cloud VPN

8 Min Read
Follow US
© 2024
activity notes activity notes
Welcome Back!

Sign in to your account

Lost your password?