grep is one of the standard Linux tools for searching text in files or command output.
Search One File
grep 'error' application.logSearch Several Files
grep 'TODO' *.pyIgnore Case
grep -i 'warning' /var/log/syslogShow Line Numbers
grep -n 'function' script.jsSearch Recursively
grep -rI 'main' ./src-r walks subdirectories and -I skips binary files.
Invert the Match
grep -v 'DEBUG' application.logThis prints lines that do not match the pattern.
Search Command Output
Pipes let grep filter another program’s output:
ps aux | grep '[n]ginx'For process names, dedicated tools such as pgrep are often better:
pgrep -a nginxAvoid Unnecessary cat
Instead of:
cat file.txt | grep 'text'prefer:
grep 'text' file.txtBoth work, but passing the filename directly is simpler.
Literal Strings
If the search text contains regex metacharacters and should be interpreted literally, use -F:
grep -F 'config[value]' file.txtConclusion
Use grep for quick text searches and combine options such as -i, -n, -r, -v, and -F to match the job. For more complex patterns, switch to extended regular expressions with grep -E.