
Search for a word in a file
If you have a file called cloudhouse.txt and you want to find all lines containing the word Python, you can use:
grep "grep" cloudhouse.txt

Commonly Used grep Options
1. Case insensitive search
The -i option enables to search for a string case insensitively in the given file. It matches the words like "grep", "Grep", "gRep".
grep -i "Grep" geekfile.txt

We can find the number of lines that matches the given string/pattern
grep -c "grep" clouhouse.txt

3. Display the Matching Filenames Using grep
We can just display the files that contains the given string/pattern.
grep -l "grep" *

4. Checking Whole Words Using grep
By default, grep matches the given string/pattern even if it is found as a substring in a file. The -w option to grep makes it match only the whole words.
grep -w "grep" cloudhouse.txt

5. Display Matched Pattern Using grep
By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.
grep -o "grep" cloudhouse.txt

6. Show Line Numbers with grep -n
To show the line number of file with the line matched.
grep -n "grep" cloudhouse.txt

7. Inverting the Pattern Match Using grep
You can display the lines that are not matched with the specified search string pattern using the -v option.
grep -v "grep" cloudhouse.txt

8. Match Lines Starting with a String using grep
The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.
grep "^grep" cloudhouse.txt

Conclusion
The grep command is a powerful and essential tool for searching text within files on Linux systems. By understanding its basic syntax and commonly used options, you can quickly locate specific information, troubleshoot issues, and analyze logs more efficiently. Whether you are a beginner or an experienced Linux user, mastering grep will significantly improve your productivity and command-line skills.
Share this article
Loading comments...
© 2026 CloudHouse Technologies Pvt.Ltd. All rights reserved.