Skip to content

grep

main purpose: search for text within files.

grep: global regular expression print

number of lines in file:
cat /etc/ssh/sshd_config | wc -l

seach for word in file:
cat /etc/ssh/sshd_config | grep Port

seach for lines without specific word in file:
cat /etc/ssh/sshd_config | grep -v Port

more directly:
grep Port /etc/ssh/sshd_config

generally, grep what where or grep word file

exclusion:
grep -v Port /etc/ssh/sshd_config

add line numbers:
grep -n Port /etc/ssh/sshd_config

count, or number, of occurences:
grep -c Port /etc/ssh/sshd_config

grep is case-sensitive:
grep port /etc/ssh/sshd_config

remove case-sensitivity:
grep -i port /etc/ssh/sshd_config

seach for word in all files in current dir:
grep word *
grep -n word *

recursive search (narrow path as much as possible):
grep -r word path
example with logs:
grep -ri Error /var/log