Simple Log File Analyzer With Bash Scripting


Overview
Ever stared at a messy log file wondering where to begin? Whether you're a developer debugging an app or a sysadmin hunting down an issue, logs hold the truth, if only you could find it quickly.
That’s why I built Simple Log File Analyzer, a lightweight Bash script that helps you extract insights from your logs in seconds. With just one command, you’ll get a breakdown of INFO
, WARNING
, and ERROR
messages, and even drill down into specific entries interactively. It’s a perfect project for anyone learning Bash or working with logs regularly.
What the Script Does
Takes a log file path as input
Validates the input and ensures the file exists and is readable
Counts the number of occurrences of
INFO
,WARNING
, andERROR
(case-insensitive)Displays a summary of the log level distribution
Offers an interactive option for the user to view all messages of a specific log level
Script Code
#!/bin/bash
# Assign the first argument to a variable
LOG_FILE="$1"
# Argument and File Validation
# Check for correct number of arguments
if [ "$#" -ne 1 ]; then
echo "Error: Expected exactly one argument, but $# was given."
echo -e "Usage:"
echo -e "\tyes$0 <log_file_path>"
exit 1
fi
# Check if file exists and is readable
if [ ! -f "$LOG_FILE" ]; then
echo "Error: File '$LOG_FILE' does not exist."
exit 1
elif [ ! -r "$LOG_FILE" ]; then
echo "Error: Access Denied. File '$LOG_FILE' is not readable."
exit 1
fi
# Log Level Counting
echo "Analyzing log file: $LOG_FILE..."
count_info=$(grep -owi "info" "$LOG_FILE" | wc -l)
count_error=$(grep -owi "error" "$LOG_FILE" | wc -l)
count_warning=$(grep -owi "warning" "$LOG_FILE" | wc -l)
# Summary Output
echo -e "\nLog Analysis Summary for $LOG_FILE:"
echo "------------------------------------"
echo "INFO messages: $count_info"
echo "ERROR messages: $count_error"
echo "WARNING messages: $count_warning"
# Optional Message Viewing
echo -e "\n" # Add a blank line for readability
read -p "Do you want to view messages of a specific type? (yes/no): " user_choice
user_choice=${user_choice,,} # Convert to lowercase
if [[ "$user_choice" == "yes" ]]; then
read -p "Enter message type (INFO, WARNING, ERROR): " msg_type
msg_type_lower=${msg_type,,} # Convert to lowercase for matching
msg_type_display="${msg_type^^}" # Convert to uppercase for display (e.g., INFO)
case "$msg_type_lower" in
"info"|"warning"|"error")
echo -e "\n------------------------------------"
echo "${msg_type_display} messages:"
grep -wi "$msg_type_lower" "$LOG_FILE"
echo "------------------------------------"
;;
*)
echo -e "\nError: Invalid message type. Please enter INFO, WARNING, or ERROR."
;;
esac
elif [[ "$user_choice" == "no" ]]; then
echo -e "\nExiting. Goodbye!"
exit 0
else
echo -e "\nError: Invalid input. Please enter 'yes' or 'no'."
fi
Use Case
This tool is especially useful for:
System administrators performing quick log audits
Developers debugging application logs
Security analysts checking for error trends or abnormal warning spikes
Key Features
Robust argument and file validation
Case-insensitive log level detection
Simple, user-friendly interaction
Easily extensible for more log levels or custom filters
Technologies Used
Bash
grep, wc, read, case, and other core Unix tools
How to Use the Script
Save the script to a file, for example,
log_
analyzer.sh
Make the script executable:
sudo chmod +x log_analyzer.sh
- Run the script with a log file path as an argument:
./log_analyzer.sh /path/to/your/logfile.log
Follow the prompts.
The script will display a summary of the log levels. You will be asked if you want to view messages of a specific type; just type
yes
orno
.
Ifyes
, enter one of:INFO
,WARNING
, orERROR
to view those messages.
Example Output
Conclusion:
Bash scripting doesn’t have to be complicated to be powerful. This project shows how a few lines of code can make your workflow faster, cleaner, and more insightful, especially when dealing with log files.
I hope this inspires you to explore automation and log parsing further. You can easily extend this script to support timestamps, export results, or analyze multiple log files at once. If you found this helpful or have ideas for improvement, feel free to connect or drop a comment below!
Subscribe to my newsletter
Read articles from Dauda Sahr N'yumah directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Dauda Sahr N'yumah
Dauda Sahr N'yumah
My name is Dauda Sahr N’yumah, and I transitioned from a background in Banking and Finance to pursue a growing passion for ethical hacking and information security. I'm currently building my skills in penetration testing, specializing in network security, and exploring tools like Kali Linux, Nmap, Metasploit, Wireshark, and Python scripting.