Using The Grep Command To Find Information

In this easy guide I will show you how to use the grep command with multiple grep examples.  If I miss anything useful or if you would like to see something added then please feel free to comment below.

First lets start off with creating a file with some dummy data, this way we have the same data and results during the guide. You can do so by running the following command:

echo "cat
dog
wolf
WOLF
DOG
fox
rabbit" > ~/mywords.txt # This will place the mywords file in your home directory

Basic File Searching

Now that we have a test file, lets start with a simple grep command. Lets say we want to look for anything in the file that contains the letter “o”. You can do so by running the following command.

root@teachmelinux:~# grep "o" ~/mywords.txt
dog
wolf
fox

As you can see above, the words WOLF and DOG were excluded from the results. This is because grep by default is case sensitive. You can do a search without case sensitivity by adding a -i to the command like the example below:

root@teachmelinux:~# grep -i "o" ~/mywords.txt
dog
wolf
WOLF
DOG
fox

Piping Into Grep

Now lets say you are running a command or script with a-lot of unimportant information being spit out. You can actually pipe the output into grep and show only the information that you want, an example is below:

root@teachmelinux:~# cat ~/mywords.txt | grep -i "wolf"
wolf
WOLF

As you can see above, this can be very useful when doing things like looking for processes, etc.

Multiple Search Terms

If you wanted to search for multiple words, for example if we wanted to search a log file for any mention of the words “error”, “fail”, or “problem”, then we would use the -E which can also be combined with -i to be even more effective. Below is another example:

root@teachmelinux:~# cat ~/mywords.txt | grep -iE "dog|wolf"
dog
wolf
WOLF
DOG
root@teachmelinux:~# cat ~/mywords.txt | grep -E "dog|wolf"
dog
wolf

You can also use regex in your searches for example, if we want anything that contains the letter “o” with any letter between “a” and “h” then the result would be something similar to the grep command example below:

root@teachmelinux:~# grep "o" ~/mywords.txt
dog
wolf
fox

root@teachmelinux:~# grep -E "o[a-h]" ~/mywords.txt
dog

Search Multiple Files

If you wanted to expand your search, for example lets say you want to check every log file for the word fail, then you can use regex (regular expressions) as-well as wildcards with grep. Here are a few quick examples:

root@teachmelinux:~# cp ~/mywords.txt ~/mywords2.txt # COPY FILE TO MAKE A SECOND FOR EXAMPLES

root@teachmelinux:~# grep "f" ~/mywords2.txt ~/mywords.txt # SEARCH SPECIFIC FILES
/root/mywords2.txt:wolf
/root/mywords2.txt:fox
/root/mywords.txt:wolf
/root/mywords.txt:fox

root@teachmelinux:~# grep "f" ~/*.txt # SEARCH ALL FILES THAT END WITH .TXT
/root/mywords2.txt:wolf
/root/mywords2.txt:fox
/root/mywords.txt:wolf
/root/mywords.txt:fox

root@teachmelinux:~# grep "f" ~/mywords[0-9].txt # SEARCH ALL FILES THAT HAVE A NUMBER 0-9
wolf
fox

Ignore Certain Results

You can also ignore certain results for example if you wanted to find any word in our text file that contains the letter “o” but you don’t care about “wolf”, you would do something similar to this example:

root@teachmelinux:~# grep "o" ~/mywords.txt
dog
wolf
fox

root@teachmelinux:~# grep "o" ~/mywords.txt | grep -v "wolf"
dog
fox

There are more possibilities with the grep command but these are the main ones that every Linux user needs to know. Please don’t forget to like/comment/share! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *