Linux › Module 5 › Lesson 2
grep, head, tail, and wc
Search and slice text with grep, head, tail, and wc
Opening
Logs are huge. Your eyes are not.
Servers generate thousands of lines per hour. grep finds needles in haystacks. head and tail show the start or end. wc counts lines, words, or bytes. Together they are log analysis 101.
1. grep — Search Text
grep pattern file grep "error" app.log grep -i fail auth.log — case insensitive grep -r "password" /etc/ — recursive (careful!) grep -n "root" /etc/passwd — show line numbers
2. head and tail
head file.txt — first 10 lines head -n 20 file.txt — first 20 tail file.txt — last 10 lines tail -f auth.log — follow live log (great during incidents)
3. wc — Count
wc -l file.txt — line count wc -w file.txt — word count wc -c file.txt — byte count
Combine them
grep "Failed password" /var/log/auth.log | tail -20 Shows only the twenty most recent failed login attempts.
Knowledge Check
grep is used to:
Multiple choice
Knowledge Check
tail -f is especially useful for:
Multiple choice
Knowledge Check
wc -l counts:
Multiple choice