LEARN. PRACTICE. EXPLAIN.
Learning paths & interview practiceP
Linux & CLI troubleshooting foundations
LESSON 2 OF 4 · ABOUT 14 MINUTES + EXERCISE

How do grep, tail, and pipes turn raw logs into answers?

THE IDEA

What it is

Standard input, standard output, and pipelines (|) let small focused tools compose cleanly without creating temporary files or altering logs. Commands like head, tail, grep, sort, uniq, and wc filter and summarize thousands of log lines in seconds.

Why you need it

Opening a 2 GB production log in an interactive editor can freeze a host or accidentally modify evidence. Streaming filters keep inspection read-only and repeatable.

How it works

  1. Inspect the shape of a file safely with head or tail before searching everything.
  2. Filter matching lines with grep (adding -i for case-insensitive or -v to invert).
  3. Pipe matches into sort | uniq -c | sort -r to rank frequent offenders, or wc -l to count.
  4. Save sanitized summaries without copying raw tokens or passwords.
Shell

Counting and ranking failed logins via pipes

grep "FAILED_LOGIN" /var/log/auth.log | wc -l

grep "FAILED_LOGIN" /var/log/auth.log | sort | uniq -c | sort -r

Each stage passes standard output to the next command’s standard input. uniq collapses adjacent duplicates, which is why sorting first is essential.

Try the in-browser Linux CLI lab

Practice ls -la, chmod, grep | wc, df -h, df -i, du, lsof, and ss -tulnp against five live troubleshooting missions.

Open Linux CLI lab

Good practices and why

  • Always sort before uniq -c so identical non-adjacent rows combine accurately.
  • Keep original log files untouched; redirect sanitized findings to a separate note or /tmp artifact.

Common mistakes

  • Running uniq without sort first and wondering why counts stay at 1.
  • Pasting unfiltered log lines containing Bearer tokens into bug tickets.
GUIDED IMPLEMENTATION

Try it yourself

Complete Mission 2 in the Linux CLI Lab: count FAILED_LOGIN entries in /var/log/auth.log, spot the leaked Bearer line, and write only the sanitized user ID to /tmp/triage-user.txt.

What a successful result looks like

Your pipeline reports exact failure counts and /tmp/triage-user.txt contains the user ID without any secret token.

0 / 6000 characters · Saved on this device. Do not include passwords or private data.

Check your understanding

Why is sort placed before uniq -c in a shell pipeline?

Answer the understanding check correctly to record exercise completion.

PUT THE IDEA UNDER PRESSURE

Related interview practice

These scenarios add constraints and follow-up questions to the concepts you just learned.

Further reading

Selected primary references · reviewed 2026-10-09. Examples are educational; use your framework’s documented integration guidance when building a deployed system.

AlifBay Prep