← Files & Text
Linux · Files & Text

How to search inside files with grep in Linux

Search file contents with grep: recursive, case-insensitive, line numbers, whole words, regex, and surrounding context.


Search for text inside files. grep prints matching lines; the common flags make it recursive and case-insensitive and add line numbers and context.

# Recursive, case-insensitive, with line numbers (skip binaries)
grep -rniI 'error' /var/log

# Whole word only
grep -rw 'oracle' /etc

# Extended regex
grep -rE 'warn|error|fatal' /var/log/messages

# 3 lines of context around each match
grep -n -C 3 'ORA-' alert.log

# Just count matches
grep -c 'ORA-' alert.log
LinuxgrepSearch