Table of Contents

About

grep stands for “global regular expression print”.

Grep searches lines of a file that match a regular expression pattern and returns them.

It is by default case sensitive.

Output

Only Filename

grep -Rl term /directory

searches all files in a directory and outputs only filenames with matched results.

Example

Basic

grep -i searchTermRegularExpression my/Path/File/RegularExpressionNameFile

where:

  • searchTermRegularExpression is the search term
  • the option -i turns the search to be case insensitive

Regular expression

And

  • Sample File
Same Line                                                       Same Line
                                                              > New Line
Same Line 2                                                     Same Line 2
Different Line                                                | Different Line :)

  • Code
cat diff.txt | grep '[\>\|]'
  • Output:
> New Line
Different Line                                                | Different Line :)

Or

See also egrep. Example: select the line that have user = ou group =

cat /etc/php-fpm.d/www.conf  | egrep 'user =|group ='
user = www-data
group = www-data

Search within a directory

  • with grep
grep -R SearchPattern  /myPath/ToADir
  • with find
find . -type f -exec grep -l "word"

with Linux - find command

Grep process without the Grep process

  • Standard Ps with Grep:
$ ps -ef | grep nqsserver
oracle    4087  4018  0 13:19 pts/0    00:00:00 grep nqsserver
oracle   23915 20779  0 Mar05 ?        00:06:28 nqsserver -quiet
  • Grep without Grep
$ ps -ef | grep nqsserver | grep -v grep
oracle   23915 20779  0 Mar05 ?        00:06:28 nqsserver -quiet
  • Grep without Grep shortcut (for grep -v)
$ ps -ef | grep [n]qsserver
oracle   23915 20779  0 Mar05 ?        00:06:28 nqsserver -quiet

Grep hidden files

Sh - Hidden files

grep pattern .*

Example

grep LD_LIBRARY_PATH .*
.bash_history:echo $LD_LIBRARY_PATH
.bash_profile:  export LD_LIBRARY_PATH=${TT_HOME}/lib:${TT_HOME}/ttoracle_home/instantclient_11_2:${LD_LIBRARY_PATH}

Or

grep -iE 'first|second'

Not

  • Don't show line with jar
cat myFile | grep -v 'jar'
  • With the L option: All ini files *SQL*.ini that doesnot have encrypt=true
grep -r -i -L --include "*.ini" "encrypt=true"  .

Keep header

  • Capture the first line with read in the variable a
  • Print it
  • and then grep the rest
ls | { IFS= read line1; echo $line1; grep fileName }

If the header has two lines

command | { IFS= read line1; read line2; echo $line1; echo $line2; grep pattern }

Documentation / Reference