Sometimes we need to search for a word in the location of a given file or in a certain directory. In such cases grep command is used to accomplish the status.
‘Grep‘ is the frequently used Unix or Linux command in Terminal. Mostly we use grep just for finding the words in a file or in a given directory. The power of grep comes with using its flags and regular expressions. We can analyze a large set of log files or directories with the help of this grep using SSH terminal.
The ‘Grep’ stands for “Global search for Regular Expressions and Print“.
The syntax of the grep command is,
Syntax: grep -flag 'string' Command: grep -r 'demoworld
In addition, to grep command’s syntax and explanation, let’s see the other ways to use the grep command in SSH
Running a last executed grep command:
This command saves a lot of time if you are executing the same search command again and again.
This command displays the last executed grep commands and also prints the result set of the command on the terminal.
Search for a string in a file
This is the basic usage of a grep command. It searches for the given string in the specified file and produces the result with highlighted search word.
Syntax: grep "Demo" newfile.txt
This command searches for the string “Demo” in the given file and prints all the lines that have the word “Demo”.
Search for a string in multiple files:
This is also one of the basic usages of the grep command. We can manually specify the list of files that we want to search or we can specify a file pattern to search for. This way help us to avoid unwanted search time, hence we have specified the file names and format.
Syntax: grep "Demo" file1 file2 grep "Demo" file_pattern