A shell script that automatically scans log files, identifies critical issues (errors, fatals, critiques), generates a structured analysis report, and alerts when thresholds are exceeded.
bash-log-analysis/
├── logs/
│ ├── application.log # App-level events (login, payments, jobs)
│ └── system.log # OS-level events (kernel, disk, memory)
│ └── bash_script.sh # Main log analysis script
│ └── log_analysis_report.txt # Auto-generated output report
- Scans all
.logfiles modified in the last 24 hours inside thelogs/directory - Searches for three severity patterns:
error,fatal,critical(case-insensitive) - Outputs all matching log lines into a structured
log_analysis_report.txt - Counts occurrences of each pattern per log file
- Alerts in the terminal if any pattern count exceeds 10
| Severity | Event |
|---|---|
| ERROR | Payment gateway timeout |
| ERROR | Insufficient disk space |
| FATAL | Out of memory in recommendation engine |
| CRITICAL | Database connection lost during backup |
| CRITICAL | Multiple failed login attempts on admin account |
| Severity | Event |
|---|---|
| ERROR | SSH brute-force failed password attempts |
| FATAL | EXT4 filesystem journal abort |
| CRITICAL | SMART disk failure on /dev/sda and /dev/sdc |
| CRITICAL | Hardware MCE (Machine Check Exception) on CPU |
Prerequisites: Bash (Linux/macOS/Git Bash on Windows)
# 1. Clone the repository
git clone https://github.com/your-username/bash-log-analysis.git
cd bash-log-analysis
# 2. Make the script executable
chmod +x bash_script.sh
# 3. Update the log_directory path in bash_script.sh to match your system
# Example (Linux/macOS): log_directory="./logs/"
# Example (Git Bash): log_directory="D:/Bash_Scripting/logs/"
# 4. Run the script
./bash_script.shThe report will be saved to log_analysis_report.txt inside the logs directory.
========================================
Analyzing log file: logs/application.log
========================================
Searching error logs in logs/application.log:
[2025-06-01 08:17:48] [ERROR] Payment gateway timeout after 30s ...
[2025-06-01 08:25:48] [ERROR] Insufficient disk space for file upload
...
Number of error logs: 9
Searching fatal logs in logs/application.log:
[2025-06-01 08:47:22] [FATAL] Out of memory error in recommendation engine
Number of fatal logs: 2
...
- Bash variables and arrays
findcommand with-mtimeflagwhileloops withIFS= read -rfor safe file iterationforloops over arraysgrepwith-i(case-insensitive) and-c(count) flags- Conditional statements (
if [ ]) - Appending output to files with
>>
This project was built while following the Bash Scripting tutorials by TechWorld with Nana on YouTube.
Nana's tutorials are an excellent resource for DevOps and scripting fundamentals — highly recommended for anyone getting started with Bash, Docker, Kubernetes, and more.
- The script currently uses a hardcoded Windows-style path (
D:/Bash_Scripting/logs/) for Git Bash compatibility. Update thelog_directoryvariable to match your local path before running. - The alert threshold is set to
10— modify the condition in the script to adjust sensitivity.
Harshad Raurale
DevOps / Cloud Enthusiast
This project is for learning purposes. Feel free to fork, modify, and build on it!
⭐ If you found this project helpful, please consider giving it a star!