grep
is a Linux command line tool used for text-searching in files. It is a
versatile tool, capable of searching through single and multiple files as well
as recursively through directories.
grep
does not utilise a search index; instead, it performs searches in
real-time and does so very quickly. It matches a pattern of characters in a
specific file or directory of files and then displays the results.
grep command with examples
The basic grep
command requires only two arguments, (i) the pattern or string
to look for and (ii) the name of the file.
grep [string] [file]
For example, to search for the word cyberspace
in the file words
:
$ grep cyberspace words
cyberspace
cyberspace's
cyberspaces
The results will display the entire line where the string is found. If there are no results, the command prints nothing.
How to find words with spaces or special characters with grep
Search patterns can be enclosed in either single or double inverted commas. This allows a search of more than one word or words with spaces or special characters.
$ grep "cyberspace's" words
cyberspace's
The same rule applies to file names. Enclose the file name in either single or double inverted commas if it contains spaces:
$ grep "cyberspace's" 'words of the world.txt'
cyberspace's
How to ignore case when searching with grep
grep
is case sensitive so search for England instead of england.
To ignore case, specify the -i
flag:
$ grep -i england words
England
England's
How to find the exact word with grep
grep
prints every match found by default.
$ grep Peter words
Peter
Peter's
Peters
Petersen
Petersen's
Peterson
Peterson's
Peters's
To find the exact word, specify the -w
flag:
$ grep -w Peter words
Peter
Peter's
How to find the line number with grep
grep
can display the line number in the results. This is useful when
searching through a large file. To display line numbers in results, specify the
-n
flag.
$ grep -n -w Peter words
14768:Peter
14769:Peter's
14770:Peters
14771:Petersen
14772:Petersen's
14773:Peterson
14774:Peterson's
14775:Peters's
Using regular expressions (regex) with grep when searching
The search query can be fine-tuned with regular expressions for more accurate
results. To search for exactly Peter and not wanting to see Peter’s, a
regex can be added. In this example, $
specifies end-of-line:
$ grep Peter$ words
Peter
Search multiple files using grep command
One grep
command can search the contents of multiple files. To query more
than one file, separate the file names with a space.
$ grep -w cyberspace$ words "words of the world.txt"
words:cyberspace
words of the world.txt:cyberspace
How to search one directory using grep command
All files in one directory can be searched by using an asterisk (*).
$ grep cyberspace *
american-english:cyberspace
british-english:cyberspace
words:cyberspace
How to search a directory recursively using grep command
To search a directory and all of its sub-directories, the -r
flag can be
used.
$ grep -r cyberspace
my-sub-dir/file-x.txt:cyberspace
american-english:cyberspace
british-english:cyberspace
words:cyberspace