Using grep command in Linux – Tutorial

Using grep command in Linux - Tutorial

The grep command is one of the most powerful and commonly used commands in Linux. It stands for Global Regular Expression Print and is used to search for text patterns within files or input streams. This tutorial will guide you through the basics and advanced usage of grep with plenty of examples.


1. Basic Syntax

The basic syntax of grep is:

grep [options] pattern [file(s)]
  • pattern: The text or regular expression you want to search for.
  • file(s): The file(s) in which to search. If no file is specified, grep reads from standard input.

2. Searching in a File

To search for a specific word or phrase in a file:

grep "hello" file.txt

This will display all lines in file.txt that contain the word “hello”.


3. Case-Insensitive Search

Use the -i option to perform a case-insensitive search:

grep -i "hello" file.txt

This will match “hello”, “Hello”, “HELLO”, etc.


4. Searching for Whole Words

To search for whole words (not substrings), use the -w option:

grep -w "hello" file.txt

This will match “hello” but not “helloo” or “hello-world”.


5. Searching in Multiple Files

You can search for a pattern in multiple files at once:

grep "hello" file1.txt file2.txt

To search all .txt files in a directory:

grep "hello" *.txt

6. Recursive Search

To search recursively through directories, use the -r option:

grep -r "hello" /path/to/directory

This will search for “hello” in all files under the specified directory.


7. Inverting the Match

To display lines that do not match the pattern, use the -v option:

grep -v "hello" file.txt

This will show all lines in file.txt that do not contain “hello”.


8. Counting Matches

To count the number of lines that match the pattern, use the -c option:

grep -c "hello" file.txt

This will output the number of lines containing “hello”.


9. Displaying Line Numbers

To display line numbers along with matching lines, use the -n option:

grep -n "hello" file.txt

This will show the line number and the matching line.


10. Using Regular Expressions

grep supports regular expressions for advanced pattern matching.

Match Lines Starting with a Pattern

grep "^hello" file.txt

This matches lines that start with “hello”.

Match Lines Ending with a Pattern

grep "world$" file.txt

This matches lines that end with “world”.

Match Any Single Character

grep "h.llo" file.txt

This matches “hello”, “hallo”, “hxllo”, etc.

Match Specific Characters

grep "h[ae]llo" file.txt

This matches “hallo” or “hello”.

Match Repeated Patterns

grep "hel*o" file.txt

This matches “helo”, “hello”, “helllo”, etc.


11. Combining grep with Other Commands

grep is often used in combination with other commands using pipes (|).

Search in Command Output

ls -l | grep "file.txt"

This lists files in the current directory and filters for “file.txt”.

Search for a Process

ps aux | grep "chrome"

This lists all running processes and filters for “chrome”.

Count Files in a Directory

ls | grep -c ".txt"

This counts the number of .txt files in the current directory.


12. Advanced Tips

Highlight Matches

Use the --color option to highlight matches:

grep --color "hello" file.txt

Search for Multiple Patterns

Use the -e option to search for multiple patterns:

grep -e "hello" -e "world" file.txt

Exclude Files from Search

Use the --exclude option to exclude specific files:

grep -r "hello" /path/to/directory --exclude="*.log"

Limit the Number of Matches

Use the -m option to limit the number of matches:

grep -m 5 "hello" file.txt

This stops after finding 5 matches.


Conclusion

The grep command is an indispensable tool for searching and filtering text in Linux. With the examples provided in this tutorial, you should be able to use grep effectively in your daily tasks. Practice these commands to become proficient and explore the man grep page for even more options and features.

Happy grepping! 🐧

Leave a Reply

Prev
Difference Between a Block and a ViewModel in Magento 2
Magento 2 Tutorial for Developers

Difference Between a Block and a ViewModel in Magento 2

When developing in Magento 2, you’ll often encounter two key concepts:

You May Also Like