Welcome back. In our previous lesson, you learned how to control access to files by managing their permissions and ownership. Now that you know how to secure and identify files, the next critical skill is to efficiently search for information within them. In the world of Capture The Flag competitions, this is a daily task. You'll be constantly sifting through log files, configuration files, source code, and even binary data, looking for flags, passwords, usernames, hidden URLs, or subtle clues.
This lesson introduces grep, the quintessential Linux tool for text searching. Its name stands for "Global Regular Expression Print," and it is one of the most powerful and frequently used commands in a security professional's toolkit. We will cover the fundamentals of using grep to find specific text patterns, explore its most useful options for refining your searches, and see how it can be combined with other commands to create powerful data-filtering pipelines.
The Basics of grep
At its core, grep reads a file line by line and prints any line that contains a specified pattern. While you might see people use cat filename | grep 'pattern', this is redundant for searching a single file. grep is designed to work on files directly.
The video below from Learn Linux TV provides a great introduction to the command, its purpose, and its basic syntax. It also demonstrates some of the most common options you'll be using.
Linux Crash Course - The grep Command
This video offers a clear and concise overview of the grep command. It covers the fundamental usage and several key options that we will explore in this lesson.
Please watch the following segments: What grep is and how it's used to search for a pattern. Inverting the search with the -v option to find lines that don't match. Searching files directly, demonstrating why cat | grep is often unnecessary. The next section from the presenter walks through several more key options using an example file: -n for line numbers, -c for counting matches, and -i for case-insensitivity. Finally, watch the section on recursive searching with -r, which is incredibly useful for searching entire directories.
Refining Your Searches with Common Options
As you just saw, grep's real power comes from its command-line options. Let's formalize the key ones you'll use most often:
-i(ignore case): Essential for finding keywords when you don't know the exact capitalization (e.g.,password,Password,PASSWORD).-n(line number): Shows the line number of each match. This is crucial for pinpointing locations in source code or configuration files.-w(whole word): Matches only whole words. Searching forgrep -w 'user'won't matchusersorusername, which can help reduce noise.-v(invert match): Prints all lines that do not contain the pattern. Perfect for filtering out irrelevant information, like commented lines in a config file (grep -v '#' config.conf).-r(recursive): Searches for the pattern in all files under the given directory. A CTF workhorse command might look likegrep -rin 'password' .to search the current directory and all subdirectories for the word 'password' while ignoring case and showing line numbers.-c(count): Instead of printing the matching lines, it prints the number of lines that contain the pattern.

For a more detailed look at these options and a few others, the following video by Corey Schafer provides excellent examples. Pay close attention to how he combines options and uses grep to search multiple files.
Linux/Mac Terminal Tutorial: The Grep Command - Search Files and Directories for Patterns of Text
This video provides a deep dive into grep's most practical options, demonstrating how to refine searches for different scenarios.
Watch the section on whole-word and case-insensitive searches, which correspond to the -w and -i flags. The next segment, from this timestamp, introduces -n for line numbers and the context flags (-B, -A, -C), which are useful for seeing the lines surrounding a match. Then, observe how to search multiple files and perform recursive searches with -r. Finally, watch this part explaining how to list only the filenames with matches (-l) or the count of matches per file (-c).
grep in the Pipeline
One of the core philosophies of the Linux command line is using small, specialized tools that work together. This is achieved through piping, where the output of one command becomes the input of another. grep is a central part of this ecosystem, acting as a powerful filter.
You've already seen how you can pipe the output of the history command into grep. This concept is universal. You can filter the output of ls, ps, find, or any other command that produces text output.

In CTFs, you'll often encounter non-text files (binaries, executables, data captures) that contain hidden text strings. A common first step is to use the strings command to extract any readable text from the file, and then pipe that output to grep to search for interesting patterns.
The following article is a walkthrough of a CTF challenge that does exactly this. It's a perfect real-world demonstration of the techniques you are learning.
How to solve my PCAP CTF challenges | Andrew Roderos
This article from Andrew Roderos' blog details the solution to a network forensics CTF challenge. It's a great example of a realistic workflow you might use in a competition.
In the first challenge walkthrough, pay close attention to how the author first uses the strings command on the packet1.pcap file. The output is noisy, so they immediately refine it by piping the output to grep -v 1234. This command uses the "invert match" flag to filter out a repetitive, uninteresting pattern, leaving only the flag. Skim through the second challenge as well. Notice the command strings packet2.pcap | grep -v 'XOn\|1234'. Here, the author uses grep -v again to filter out multiple noisy patterns at once. We'll cover the \| syntax in our next lesson on regular expressions.
For a comprehensive text reference that covers all the options we've discussed and more, "Grep by example" is an excellent resource. You can refer to it now or in the future when you need to look up a specific option.
Grep by example: Interactive guide
This interactive guide provides clear, runnable examples for grep's various features. It's a fantastic reference to bookmark.
Focus on these sections for now: Read the Basics section for a fundamental overview. The Matches section explains partial vs. whole word (-w) and whole line (-x) matching. Under "Search options", review the sections for Ignore case (-i) and Inverse matching (-v). Under "Output options", review Count matches (-c) and Show matches only (-o).
Conclusion
In this lesson, you've added a critical tool, grep, to your command-line arsenal. You learned its basic syntax for searching text within files and explored the most important flags—-i, -n, -w, -v, -r, and -c—to make your searches more precise and effective. Most importantly, you saw how grep functions as a filter in a command pipeline, allowing you to sift through the output of other tools to find exactly what you need, a technique you will use constantly in CTFs.
So far, we've only searched for simple, fixed strings. But grep's true power lies in its ability to search for complex patterns using regular expressions. In our next lesson, we will dive into the world of regular expressions, unlocking a new level of search capability with grep.