The grep command in UNIX-like operating systems is used to search for a given substring or regular expression in text files or the output of other commands.
The syntax of the grep command is as follows:
grep [options] sample [files]
Here, pattern is the substring or regular expression to be searched and [files]
is the list of files to be searched. If [files]
is not specified, grep will read input from standard input (usually meaning that you can pass the output of other commands to grep
).
Some common grep
options are:
-i
- ignore character case when searching
-r
- search recursively in all subdirectories
-n
- output line numbers where a pattern is found
-v
- output only lines that do not contain the pattern
For example, to find all lines in the file example.txt containing the word "apple", you can use the command:
grep apple example.txt
And to find all files in the current directory and its subdirectories containing the word "banana", you can use the command:
grep -r banana .
Here the dot stands for the current directory.