grep is one of the standard Linux tools for searching text in files or command output.
Search One File
grep 'error' application.log
Search Several Files
grep 'TODO' *.py
Ignore Case
grep -i 'warning' /var/log/syslog
Show Line Numbers
grep -n 'function' script.js
Search Recursively
grep -rI 'main' ./src
-r walks subdirectories and -I skips binary files.
Invert the Match
grep -v 'DEBUG' application.log
This 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 nginx
Avoid Unnecessary cat
Instead of:
cat file.txt | grep 'text'
prefer:
grep 'text' file.txt
Both 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.txt
Conclusion
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.