grep and Its Flags

What this is. grep pattern file prints every line of the file containing the pattern. That is the whole idea, and the flags are what make it powerful.

-i ignores capitalisation.

-n prints line numbers.

-r searches a whole directory tree recursively.

-v inverts, printing lines that do not match.

-c prints a count instead of the lines.

find and grep answer different questions

FIND

find . -name "*.js"

WHERE is the file

GREP

grep -r "TODO" .

WHAT is inside the files
Compare → People mix these up constantly. find matches on the name of a file. grep matches on the contents of a file. When you cannot remember which file you wrote something in, grep is the one you want.
SANKOFA SANDBOX
learner:~$

The projects here are seeded with TODO comments and the logs with errors.

Try it → Run `grep TODO projects/archive/notes.md`, then `grep -n TODO projects/archive/notes.md`, then `grep -c TODO projects/archive/notes.md`. Same search, three different answers.

The combination you will use most. grep -rn "TODO" . searches every file beneath the current directory, prints the filename and the line number for each hit, and is genuinely how developers find things in unfamiliar code. Adding -i on top makes it case insensitive, giving grep -rni, which is a sequence worth committing to muscle memory.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Inverting, Counting, and Combining →
← Back