Grep Command in Unix/Linux with 11 Simple Examples
Description for grep command in Unix/Linux
Grep command in Unix/Linux is a powerful tool that searches for matching a regular expression against text in a file, multiple files or a stream of input. It searches for the pattern of text that you specify on the command line and prints output for you.
In addition, three variant programs egrep,fgrep and rgrep are available.
- egrep is the same as grep -E
- fgrep is the same as grep -F
- rgrep is the same as grep -r
Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications that rely on them to run unmodified.
Example of grep command in Unix/Linux: Let’s say if you quickly want to search the string “linux” in .html files on your machine? Let’s start by searching a single file.
Here, our PATTERN string is “linux” and our FILE is index.html
1. Search for a string “linux” in a file using grep command in unix
This is the basic usage of grep command. It searches for the given string in the specified file.
grep “linux” index.html
2. Insensitive case search with grep -i
The below grep command searches for the words like “LINUX”, “Linux”, “linux” case insensitively.
grep -i “linux” index.html
3. Searching for a string in multiple files.
This command will search for "linux" string in multiple files at a time. It searches in all files with file1.txt, file2.txt and along with different extensions too like file1.html, file2.php and so on.
grep “linux” file*.*
4. Specifying the search string as a regular expression pattern.
It is a very powerful feature and can use as a regular expression with much effectively. In the below example, it searches for all the pattern that starts with “fast” and ends with “host” with anything in-between. i.e To search “fast[anything in-between]host” in index.html file.
grep “fast.*host” index.html
5. Displaying the line numbers.
You can use this grep command to display the line number which contains the matched string in a file using the -n option
grep -n “word*” file.txt
6. Highlighting the search using grep
If we use the –color option, our successful matches will be highlighted for us.
grep –color “linux” index.html
7. Print the line excluding the pattern using -v option
List all the lines of the file /etc/passwd that does not contain specific word “string”.
grep -v linux /etc/passwd
8. Display all the lines that starts with specified pattern using ^ symbol
Bash shell treats carrot symbol (^) as a special character which treat as the beginning of line. Let’s display the lines which starts with “root” word in the file /etc/passwd.
grep ^root /etc/passwd
9. Display all the lines that ends with specified pattern using $ symbol.
List all the lines of /etc/passwd that ends with “bash” word.
grep bash$ /etc/passwd
10. Search the pattern recursively using -r option
The below command will search linux in the “/etc” directory recursively.
grep -r linux /etc/
11. Counting the lines when words match
This grep command can report the number of times the pattern matches for each file by using -c (count) option.
grep -c 'test' /home/example/test.txt
By the time you complete reading this article, I am sure you will get basic idea about grep command and how to use it on your Linux server with different search patterns. If you like this, please post your comment below.