Become the Log Master by:
- Finding the number of
ERROR
,WARNING
, andINFO
messages from log files - Creating a summary report
- Archiving the logs with a timestamp
- Bonus: Compressing and cleaning up old log files
- Run this command to generate sample log files:
mkdir -p log-master/logs && cd log-master/logs
for i in {1..5}; do
for j in {1..10}; do
echo "$(date '+%Y-%m-%d %H:%M:%S') [INFO] Service started" >> app$i.log
echo "$(date '+%Y-%m-%d %H:%M:%S') [WARNING] Memory usage high" >> app$i.log
echo "$(date '+%Y-%m-%d %H:%M:%S') [ERROR] Failed DB connection" >> app$i.log
done
done
cd ../..
echo "β
5 log files generated in log-master/logs/"
Youβll work inside the
log-master/
directory.
Write commands to count how many times each log level appears across all logs.
Example:
grep -roh 'ERROR' logs/ | wc -l
grep -roh 'WARNING' logs/ | wc -l
grep -roh 'INFO' logs/ | wc -l
π§ Hint: Try using cut
, sort
, and uniq -c
if the logs were more complex!
Write the output to a file called log_summary.txt
like this:
ERROR: 4
WARNING: 3
INFO: 5
Use a script or compound command:
{
echo "ERROR: $(grep -roh 'ERROR' logs/ | wc -l)"
echo "WARNING: $(grep -roh 'WARNING' logs/ | wc -l)"
echo "INFO: $(grep -roh 'INFO' logs/ | wc -l)"
} > log_summary.txt
Compress the entire logs/
folder into a tarball named with todayβs date:
tar -czvf logs_$(date +%F).tar.gz logs/
Delete original .log
files after compression (simulate archiving + cleaning):
rm -rf logs/
- Sort logs by frequency of log level:
sort | uniq -c | sort -nr
- Combine everything into a bash script called
process_logs.sh
- Use
awk
to extract timestamps and group by hour (advanced)
Skill | Real-World Use Case |
---|---|
grep , wc |
Analyze service logs for errors/warnings |
tar , gzip |
Archive rotated logs before deletion |
uniq -c , sort |
Frequency analysis of log patterns |
Log reporting | Basis for alerting, monitoring, analytics |
- Fastest to complete all 3 tasks correctly
- Neatest summary file
- Most efficient (fewest commands)
- Bonus: One-liner challenge!
π Winner earns the prestigious title of βLog Master of SIT Puneβ π₯