Grep with Different Options — Linux

Kithmini Oshadi
4 min readOct 6, 2019

--

Grep is a simple command in Linux but you can do amazing searches through your files using that with different options. It’s useful to search a word or a word pattern in a particular file or in files inside a given directory. GREP simply means:

There are several ways to use grep command according to our necessity. Mainly we need to search a word pattern in…

  1. a particular file or files
  2. each file in a given directory (including sub directories)

1- How to search a word pattern in a particular file or files?

Particular file/s path should mention in the command;

For multiple files search;

2- How to search a word pattern in each file in a given directory (including sub directories)?

Go to the relevant directory in Linux command line and enter following command;

Further we can use options with any of the above mentioned way according to our requirement. For the command syntax I’ve considered the above mentioned second method’s use cases with different options. You can use these options with the first method as well.

  1. Case insensitive search (not consider uppercase/lowercase)

grep -ri “<searchWord>”
ex: grep -ri “myJsonTopic”

As You can see, this command has searched myJsonTopic, myJSONTopic, MYJSONTOPIC , myJsonTopicTest contained lines without considering the uppercase or lowercase.

2. List down only the file names which contains particular word

grep -rl “<searchWord>”
ex: grep -rl “myJsonTopic”

Use this when you need to get only the file names which contains a particular word.

This shows only the file name without separately showing the “myJsonTopic” contained lines.

3. Search only the whole word except sub strings

grep -rw “<searchWord>”
ex: grep -rw “myJsonTopic”

This has not searched ‘myJsonTopicTest ‘ contained line as ‘myJsonTopic’ word is a substring in it.

4. Show line number along with the searched String

grep -rn “<searchWord>”
ex: grep -rn “myJsonTopic”
When you need to get the searched line number, -n option is useful.

5. Show lines that are not matched with the searched String

grep -rv “<searchWord>”
ex: grep -rv “myJsonTopic”
Use -v option to avoid a particular word contained lines and to get other lines.

In the above example you can see, it has searched lines except “myJsonTopic” contained lines.

6. Show matching lines which starts with the given word

grep -r “<^searchWord>”
ex: grep -r “^Feature”
This will search lines starting with the word “Feature”

7. Show matching lines which ends with the given word

grep -r “<searchWord$>”
ex: grep -r “Wiremocks$”
This will search lines ends with the word “Wiremocks”

8. Show matching lines with multiple search words

grep -re “<searchWord1>” -e “<searchWord2>” -e “<searchWord3>”
ex: grep -re “Feature” -e “Wiremocks”
Assume you need to search multiple words. For that you can easily mention the words using -e option.

9. Show count of lines matches the given word. Show in files separately.

grep -rc “<searchWord>”
ex: grep -rc “my-project”

This shows line count contained “my-project” word and shows line count separately for each file.

10. Show full count of lines matches the given word. Show sum of count in all files.

grep -rch “<searchWord>” | paste -sd+ — | bc
ex: grep -rch “myJsonTopic” | paste -sd+ — | bc
When you need to get the total line count contains a given word in a directory (including sub directories), easily use this command.

11. Search and Rename a word in each place inside directory

grep -rl “<searchWord>” * -R | xargs sed -i ‘s/<searchWord>/<updateWord>/g’
ex: grep -rl “myJsonTopic” * -R | xargs sed -i ‘s/myJsonTopic/renamedTopic/g’

In the above example, 5 lines have searched with “myJsonTopic” and 0 lines with “renamedTopic” before enter the rename command (mentioned in green square). After the command you can see it has renamed correctly as there are no matching lines for grep “myJsonTopic” and 5 lines searched for grep “renamedTopic”.

It’s time for you to play with Grep! 😁

--

--