How to Use Grep (linux) and findstr (windows)

The grep command is very useful for search for strings and words in a file from the command line interface.

In windows we can use findstr to search for strings and words in the command prompt.

GREP

Usage: grep [OPTION]… PATTERN [FILE]

  • -o  searches for only the string
  • -v searches for all words except the string .It is an inverted search
  • -c counts the number of matching words for the string

grep “shepherd” filetosearch.txt

grepbasicjsecurity

grep -o “He restoreth” filetosearch.txt

greponlyjsecurity

grep -v “want” filetosearch.txt

grepinvertjsecurity

grep -c “” filetosearch.txt

grepcountjsecurity

We can also use grep for some system adminstration task

  • top | grep firefox searches for firefox process in top
  • netstat | grep tcp searches for firefox process in top
  • ps -ef | grep firefox searches for firefox process
  • df -h | grep sda*searches for all drives beginning with sda process in top

greptopnetstatsysadmingrepsysadmin2

FINDSTR in windows

findstr short for findstring works like grep in linux and it is used to search or find strings

  • tasklist | findstr firefox

Leave a Comment

Your email address will not be published. Required fields are marked *