Skip to main content

First Grep picoCTF 2019 Solution

Search through a large text file to find the hidden flag using command-line tools.

Published: April 2, 2026Updated: August 13, 2026

Description

Can you find the flag in file? Something tells me there is a better way than printing the whole thing.

Download the file named 'file' from the challenge page.

Solution

Want to try it yourself first?

The guided walkthrough reveals hints one step at a time.

Walk me through it
  1. Step 1Search the file with grep
    Observation
    The description says there is a better way than printing the whole file, and the challenge is called 'first grep'. That names the tool outright: search for the flag pattern instead of reading every line.
    Rather than printing the entire file, use grep to search for lines matching 'picoCTF'. grep scans the file line by line and prints only matching lines, making it instant even in very large files.
    bash
    grep "picoCTF" file

    Expected output

    picoCTF{...}
    What didn't work first

    Tried: Open the file in a text editor like nano or cat the whole file to the terminal and scroll manually.

    The file is big enough that scrolling is slow and easy to get wrong, and the hint says as much. grep scans every line instantly and prints only the match, so the flag arrives without reading thousands of unrelated lines.

    Tried: Run strings file instead of grep to look for readable text including the flag.

    strings dumps every printable sequence in the file, which on a large file is still hundreds of lines to sift through. grep with the picoCTF pattern filters to exactly the flag line, so it is the precise tool rather than a broad extraction pass.

    Learn more

    grep (Global Regular Expression Print) is one of the most fundamental Unix command-line tools. It reads a file (or stdin) line by line and prints every line that matches a given pattern. The pattern can be a plain string, a basic regular expression (BRE), or an extended regular expression (ERE with grep -E or egrep).

    In CTF work, grep is used constantly. Common patterns include:

    • grep "picoCTF" file - find the flag in a file
    • grep -r "password" . - recursively search all files in a directory
    • grep -i "flag" file - case-insensitive search
    • grep -a "picoCTF" binary - treat binary file as text
    • strings binary | grep "picoCTF" - combine with strings for binaries

    Performance-wise, grep is extremely fast because it uses highly optimized string matching algorithms (Boyer-Moore-Horspool, SIMD instructions in modern implementations). On a gigabyte-sized log file, grep can find your pattern in seconds while opening the file in a text editor might crash the system. This makes it invaluable for log analysis, forensics, and source code auditing in real security work.

    For more powerful searching in codebases, ripgrep (rg) is a modern alternative that is typically 5-10x faster than grep, respects .gitignore, and handles encoding edge cases better. Both tools accept the same basic pattern syntax.

    Regular expressions with grep unlock far more powerful searches. With grep -E (extended regex), you can match patterns like picoCTF{[a-zA-Z0-9_]+} to find any properly formatted flag, or [0-9]{1,3}\.[0-9]{1,3} to find IP address-like patterns. The grep -P flag enables Perl-compatible regular expressions (PCRE), which adds lookaheads, lookbehinds, and other advanced constructs. Learning basic regex is one of the highest-return skills in security: it applies to log analysis, intrusion detection rule writing, YARA malware signatures, and web application firewall rules.

    grep in security auditing is indispensable for source code review. When auditing a web application for SQL injection, you might run grep -rn "SELECT.*\$" . to find all SQL queries that interpolate variables directly. For command injection, grep -rn "exec|system|shell_exec" . finds dangerous function calls. For hardcoded secrets, grep -rni "password\s*=" . catches obvious cases. These patterns are not foolproof - a sophisticated attacker would obfuscate their code - but they catch a large proportion of real vulnerabilities in quickly-written code.

    Key grep flags to memorize for CTF and security work: -c (count matches instead of printing them), -l (list only file names with matches), -n (show line numbers), -o (print only the matching portion of each line, not the whole line - extremely useful for extracting flag patterns), -v (invert match, show lines that do NOT match), and -A/-B/-C (show N lines After/Before/around a match for context). The -o flag combined with a regex is particularly powerful: grep -oP 'picoCTF{[^}]+}' file extracts the flag directly.

Interactive tools
  • Strings ExtractorPull printable text from any binary, library, or image. ASCII and UTF-16 detection, configurable minimum length, flag-like highlight, no command line needed.

Flag

Reveal flag

picoCTF{grep_is_good_to_find_things_...}

grep (Global Regular Expression Print) scans files line by line for pattern matches - one of the most-used tools in CTF work.

Key takeaway

grep is the universal tool for pattern search in text: it reads line by line and emits only matches, scaling to gigabyte logs in seconds. The same primitive powers security audits, where a recursive grep over a codebase surfaces SQL injection sinks, dangerous function calls, and hardcoded credentials faster than manual review. Add regular expressions and the -o flag and it will pull structured tokens like IP addresses or JWT payloads straight out of raw output.

Related reading

Useful tools for General Skills

Where to go next