Skip to content

Instantly share code, notes, and snippets.

@thirukkural2022
Created December 1, 2015 08:34
Show Gist options
  • Save thirukkural2022/0c70b70bb5a3f23f9160 to your computer and use it in GitHub Desktop.
Save thirukkural2022/0c70b70bb5a3f23f9160 to your computer and use it in GitHub Desktop.
unix - grep basics
# http://www.cyberciti.biz/faq/howto-search-find-file-for-text-string/
The syntax is:
grep "text string to search” directory-path
OR
grep [option] "text string to search” directory-path
OR
grep -r "text string to search” directory-path
OR
grep -r -H "text string to search” directory-path
OR
egrep -R "word-1|word-2” directory-path
OR
egrep -w -R "word-1|word-2” directory-path
$ grep "redeem reward" /home/tom/*.txt
$ grep "redeem reward" ~/*.txt
$ grep -r "redeem reward" /home/tom/
$ grep -R "redeem reward" /home/tom/
$ grep -H -r "redeem reward" /home/tom #display only file name
#to just display the file name:
$ grep -H -R vivek /etc/* | cut -d: -f1
#suppress file names
$ grep -h -R 'main()' ~/projects/*.c
#display only words
$ grep -w -R 'getMyData()' ~/projects/
#search two or more words
$ egrep -w -R 'word1|word2' ~/projects/
#hide all errors and warnings
grep -w -R 'getMyData()' ~/projects/ 2>/dev/null
#display matched text in color
rep --color 'word' file
grep --color -R 'word' /path/to/dir
grep --color -R "192.168.1.5" /etc/
grep --color -R -h "192.168.1.5" /etc/
grep --color -R -h "192.168.1.5" /etc/ 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment