📖 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
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
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
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
Hint
Use echo "Number of error lines: $error_count" to display the result.
Practice
(1/5)
1. What is the primary purpose of files located in the /var/log/ directory on a Linux system?
easy
A. To hold user personal documents and files
B. To contain system configuration files
C. To save temporary files created during program execution
D. To store system and application logs recording events and errors
Solution
Step 1: Understand the role of /var/log/
The /var/log/ directory is designed to keep logs about system and application activities.
Step 2: Differentiate from other directories
Other directories like /home store user files, /etc holds configs, and /tmp stores temporary files.
Final Answer:
To store system and application logs recording events and errors -> Option D
Quick Check:
/var/log/ = system logs [OK]
Hint: Remember: /var/log holds logs, not configs or user files [OK]
Common Mistakes:
Confusing /var/log with /etc for configs
Thinking /var/log stores user documents
Mixing up temporary files with logs
2. Which command correctly displays the last 10 lines of the /var/log/syslog file?
easy
A. tail /var/log/syslog
B. head /var/log/syslog
C. cat /var/log/syslog | head
D. less /var/log/syslog
Solution
Step 1: Recall command functions
The tail command shows the last lines of a file; by default, it shows the last 10 lines.
Step 2: Compare options
head shows the first lines, cat | head also shows first lines, and less opens a pager for browsing but does not directly show last lines.
Final Answer:
tail /var/log/syslog -> Option A
Quick Check:
tail = last 10 lines [OK]
Hint: Use tail to see last lines quickly [OK]
Common Mistakes:
Using head instead of tail for last lines
Confusing less with tail output
Piping cat to head for last lines
3. What will be the output of the command grep 'error' /var/log/syslog | wc -l?
medium
A. It displays all lines from /var/log/syslog except those with 'error'
B. It shows the number of lines containing the word 'error' in /var/log/syslog
C. It counts all words in /var/log/syslog
D. It shows the first line containing 'error' in /var/log/syslog
Solution
Step 1: Understand grep and wc -l
grep 'error' /var/log/syslog filters lines containing 'error'. wc -l counts the number of lines from that output.
Step 2: Combine command effects
The pipeline counts how many lines in syslog have the word 'error'.
Final Answer:
It shows the number of lines containing the word 'error' in /var/log/syslog -> Option B
Quick Check:
grep + wc -l = count matching lines [OK]
Hint: grep filters lines; wc -l counts them [OK]
Common Mistakes:
Thinking grep excludes matching lines
Confusing wc -l with word count
Assuming output shows lines, not count
4. You run tail -n 20 /var/log/auth.log but get an error: "tail: cannot open '/var/log/auth.log' for reading: Permission denied". What is the best way to fix this?
medium
A. Run the command with sudo: sudo tail -n 20 /var/log/auth.log
B. Change the file permissions with chmod 777 /var/log/auth.log
C. Copy the file to your home directory and run tail there
D. Use head instead of tail
Solution
Step 1: Identify permission issue
The error says permission denied, meaning the user lacks rights to read the file.
Step 2: Use elevated privileges safely
Using sudo runs the command as root, allowing access without changing file permissions.
Final Answer:
Run the command with sudo: sudo tail -n 20 /var/log/auth.log -> Option A
Quick Check:
Permission denied? Use sudo [OK]
Hint: Use sudo for restricted log files [OK]
Common Mistakes:
Changing permissions insecurely with chmod 777
Copying files without permissions
Using head instead of fixing permissions
5. You want to monitor new entries added to /var/log/syslog in real-time and filter only lines containing the word 'failed'. Which command achieves this?
hard
A. cat /var/log/syslog | tail -f
B. grep failed /var/log/syslog
C. tail -f /var/log/syslog | grep failed
D. less /var/log/syslog | grep failed
Solution
Step 1: Understand real-time monitoring
tail -f shows new lines added to a file live as they appear.
Step 2: Filter output for 'failed'
Piping tail -f output to grep failed shows only lines containing 'failed' in real-time.
Final Answer:
tail -f /var/log/syslog | grep failed -> Option C
Quick Check:
tail -f + grep = live filtered log [OK]
Hint: Use tail -f piped to grep for live filtered logs [OK]
Common Mistakes:
Using grep alone shows only current matches, no live updates
Using cat with tail -f incorrectly
Trying to grep inside less which is not live
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.