grep

Definition:

The command grep stands for “global regular expression print”, and is used to search for specified text patterns in files or program outputs.

Syntax:

grep [option(s)] pattern [file(s)]

Options:

Option Description
-E (extended regexp) Causes grep to behave like egrep.
-F (fixed strings) Causes grep to behave like fgrep.
-G (basic regexp) Causes grep, egrep, or fgrep to behave like the standard grep utility.
-r To search recursively through an entire directory tree (i.e., a directory and all levels of subdirectories within it)
-I Process a binary file as if it did not contain matching data.
-c To report the number of times that the pattern has been matched for each file and to not display the actual lines.
-n To precede each line of output with the number of the line in the text file from which it was obtained.
-v It matches only those lines that do not contain the given pattern.
-w To select only those lines that contain an entire word or phrase that matches the specified pattern.
-x To select only those lines that match exactly the specified pattern.
-l To not return the lines containing matches but to only return only the names of the files that contain matches.
-L It is the opposite of the -l option (and analogous to the -v option) i.e. it will cause grep to return only the names of files that do not contain the specified pattern.

Example:

  • This would search all files in the current directory and in all of its subdirectories, for every line containing the string “Educative”:
grep -r 'Educative' *

Get hands-on with 1200+ tech skills courses.