Overview
Once you can find and read files, the next skill is processing text. Linux ships with a toolbox of small, sharp commands that search, filter, transform, and combine text — and they become powerful when you connect them with pipes. In CTFs these tools turn a giant log, a wordlist, or a dump of output into the one line that contains the flag.
Learning objectives
- Search text with
grep, including recursive and case-insensitive searches. - Combine commands with pipes (
|) and redirect output to files. - Slice and reshape text with
cut,sort,uniq,wc, andtr. - Extract flags and patterns efficiently from large or messy input.
Core concept
The Unix philosophy is to build small tools that each do one thing well and then compose them. The glue is the pipe (|), which sends the output of one command as the input of the next. A chain like cat data | grep flag | sort | uniq reads a file, keeps matching lines, sorts them, and removes duplicates — each stage doing one job. Mastering a few of these tools and the pipe lets you answer almost any "find the needle in the text" question.
Two related ideas are redirection — > writes output to a file (overwriting) and >> appends — and standard streams, where normal output (stdout) and errors (stderr) are separate channels you can route independently.
How it works
grep <pattern> <file>prints lines matching a pattern.grep -iignores case,grep -rsearches recursively through directories,grep -nshows line numbers, andgrep -vinverts the match (lines that do not match). Patterns can be regular expressions withgrep -E.cutextracts columns:cut -d: -f1 /etc/passwdsplits each line on:and keeps field 1.sortorders lines;sort -nsorts numerically;sort -rreverses.uniqcollapses adjacent duplicate lines (so you usuallysortfirst);uniq -ccounts occurrences.wccounts:wc -lcounts lines,wc -wwords,wc -cbytes.trtranslates or deletes characters:tr 'a-z' 'A-Z'uppercases;tr -d '\n'strips newlines.head/tailshow the first/last lines (from the Files lesson) and pair well with pipes.
Practical example
A challenge hands you a noisy log with thousands of lines and says the flag was logged once. You do not want to read it by hand:
$ wc -l server.log
20431 server.log
$ grep -i "flag{" server.log
2026-09-25T10:04:11 INFO issued token flag{gr3p_finds_the_needle}
One grep reduced twenty thousand lines to the single one that mattered. When the interesting data is buried in a structured field, you slice it out:
$ grep "user=" access.log | cut -d= -f2 | sort | uniq -c | sort -rn | head -3
412 admin
118 guest
3 flag_bot
That pipeline extracted the value after user=, tallied how often each appeared, and showed the top three — a classic way to spot an anomaly. You could save any result for later with redirection: grep -i "flag{" server.log > found.txt.
Cybersecurity relevance
Text processing is the daily bread of security work. Log analysis during incident response, sifting through wordlists, parsing scan output, extracting credentials or tokens from a data dump, and building quick one-off filters all rely on grep, pipes, and friends. The ability to compose a short pipeline on the spot — rather than opening a heavyweight tool — is what makes an analyst fast. In CTFs, many challenges are won or lost on whether you can carve the flag out of a large or ugly blob of text efficiently and precisely.
Common mistakes
- Using
uniqwithout sorting first, so non-adjacent duplicates survive. - Forgetting
grep -rwhen the target might be spread across many files in a directory tree. - Overwriting with
>when you meant to append with>>, destroying earlier results. - Writing a regex without
-E(or not escaping special characters), so the pattern does not match what you expect.
Key takeaways
- Pipes (
|) compose small single-purpose tools into powerful text-processing chains. grepsearches text (-iignore case,-rrecursive,-vinvert,-nline numbers,-Eregex).cut,sort,uniq,wc, andtrslice, order, deduplicate, count, and translate text.>overwrites and>>appends output to a file;sortbeforeuniq, and reach for a pipeline before reading by hand.
Check your understanding
- Why must you usually run
sortbeforeuniqto remove duplicate lines? - Which single command would find every line containing
passwordacross all files under a directory, ignoring case? - What is the difference between redirecting output with
>and with>>, and when does the difference matter?

