0
0
Linux CLIscripting~15 mins

System logs (/var/log) in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
System Logs Analysis with Linux CLI
📖 Scenario: You are a system administrator who needs to check system logs stored in the /var/log directory. These logs help you understand what is happening on your Linux system.One common task is to find error messages in the system log file /var/log/syslog.
🎯 Goal: Build a simple script that reads the /var/log/syslog file, filters out lines containing the word error (case insensitive), and counts how many error lines are found.
📋 What You'll Learn
Create a variable holding the path to /var/log/syslog
Create a variable holding the keyword error
Use grep with the keyword variable to filter lines from the log file
Count the number of matching lines
Print the count with a clear message
💡 Why This Matters
🌍 Real World
System administrators often need to check logs to find errors or warnings quickly. This script helps automate that task.
💼 Career
Knowing how to use Linux command line tools like grep and wc is essential for troubleshooting and monitoring servers.
Progress0 / 4 steps
1
Set the log file path
Create a variable called logfile and set it to the string /var/log/syslog.
Linux CLI
Need a hint?

Use logfile="/var/log/syslog" to store the log file path.

2
Set the keyword to search
Create a variable called keyword and set it to the string error.
Linux CLI
Need a hint?

Use keyword="error" to store the search word.

3
Filter and count error lines
Use grep with the -i option and the variable keyword to filter lines from the file stored in logfile. Then use wc -l to count the matching lines. Store the count in a variable called error_count.
Linux CLI
Need a hint?

Use error_count=$(grep -i "$keyword" "$logfile" | wc -l) to count error lines.

4
Print the error count
Print the message "Number of error lines: " followed by the value of error_count using echo.
Linux CLI
Need a hint?

Use echo "Number of error lines: $error_count" to display the result.