How does grep work for searching text?

Answer

grep pattern file searches for lines matching a regular expression pattern and prints matching lines. Key flags: -i (case-insensitive), -r (recursive directory search), -n (show line numbers), -v (invert match — show non-matching lines), -c (count matching lines), -l (list only file names), -E (extended regex, same as egrep). Example: grep -rn "TODO" ./src/ finds all TODOs in a codebase. Pipe output: dmesg | grep -i error filters kernel messages for errors. grep is one of the most frequently used Linux commands in daily sysadmin and development work.