Linux grep command with example

grep command is used to search for specific patterns within a file or a set of files. Here is an example of how to use the grep command: Suppose we have a file named "example.txt" that contains the following data:

This is an example file.
It contains several lines of text.
Each line has some words on it.
  1. To search for the word "example" within this file, we can use the following command:
grep "example" example.txt

This will return the following output:

This is an example file.
  1. The grep command searched through the file "example.txt" and found the line that contains the word "example". we can also use regular expressions with grep. For example, to search for lines that start with the word "This", we can use the following command:
grep "^This" example.txt

This will return the following output:

This is an example file.
  1. The ^ symbol in the regular expression matches the beginning of a line. we can also use the -i option to perform a case-insensitive search. For example, to search for the word "contains" regardless of whether it is capitalized, we can use the following command:
grep -i "contains" example.txt

This will return the following output:

It contains several lines of text.
  1. Searching for multiple patterns we can use the grep command to search for multiple patterns in a file. For example, suppose we want to search for lines that contain both the words "example" and "file". we can use the following command:
grep "example.*file" example.txt

This will return the following output:

This is an example file.

The .* in the regular expression matches any character (except newline) zero or more times.

  1. Searching for a pattern in a directory we can use the grep command to search for a pattern in all files within a directory. For example, suppose we want to search for the word "example" in all files within the current directory. we can use the following command:
grep "example" *

This will return the names of all files that contain the word "example", as well as the lines in those files that contain the word.

  1. Counting the number of matches we can use the grep command to count the number of matches of a pattern in a file. For example, suppose we want to count the number of times the word "example" appears in the file "example.txt". we can use the following command:
grep -c "example" example.txt

This will return the number of times the word "example" appears in the file "example.txt".

  1. Searching for a pattern in a compressed file we can use the grep command to search for a pattern in a compressed file (e.g. a file with a .gz extension). For example, suppose we have a compressed file named "example.txt.gz" and we want to search for the word "example" within it. we can use the following command:
grep "example" example.txt.gz

This will return the lines in the compressed file that contain the word "example".

  1. Searching for lines that do not match a pattern we can use the -v option to search for lines that do not match a pattern. For example, suppose we want to search for lines in "example.txt" that do not contain the word "example". we can use the following command:
grep -v "example" example.txt

This will return the following output:

It contains several lines of text.
Each line has some words on it.
  1. Searching for a pattern in a specific file type we can use the --include option to search for a pattern in files that match a specific file type. For example, suppose we want to search for the word "example" in all .txt files in the current directory. we can use the following command:
grep "example" --include "*.txt" *

This will search for the word "example" in all .txt files in the current directory.

  1. Searching for a pattern in multiple files we can use the grep command to search for a pattern in multiple files. For example, suppose we want to search for the word "example" in both "example.txt" and "example2.txt". we can use the following command:
grep "example" example.txt example2.txt

This will search for the word "example" in both "example.txt" and "example2.txt".

  1. Ignoring binary files If we want to search for a pattern in files but ignore binary files, we can use the -I option. For example, suppose we want to search for the word "example" in all files in the current directory, but ignore binary files. we can use the following command:
grep -I "example" *

This will search for the word "example" in all files in the current directory, but ignore binary files.

  1. Searching for a pattern recursively in a directory we can use the -r option to search for a pattern recursively in a directory and all its subdirectories. For example, suppose we want to search for the word "example" in all files within a directory named "mydir". we can use the following command:
grep -r "example" mydir/

This will search for the word "example" in all files within the directory "mydir" and its subdirectories.

  1. Displaying context around a match we can use the -C option to display context around a match. For example, suppose we want to search for the word "example" in a file named "example.txt" and display the two lines before and after each match. we can use the following command:
grep -C 2 "example" example.txt

This will display the two lines before and after each match of the word "example" in "example.txt".

  1. Searching for a pattern in a case-insensitive manner we can use the -i option to search for a pattern in a case-insensitive manner. For example, suppose we want to search for the word "example" in a file named "example.txt" but we're not sure if it's capitalized or not. we can use the following command:
grep -i "example" example.txt

This will search for the word "example" in "example.txt" regardless of whether it's capitalized or not.

  1. Using regular expressions to search for patterns we can use regular expressions to search for more complex patterns in files. For example, suppose we want to search for lines in a file named "example.txt" that contain three consecutive digits. we can use the following command:
grep '[0-9]\{3\}' example.txt

This will search for lines in "example.txt" that contain three consecutive digits.

  1. Counting the number of matches We can use the -c option to count the number of matches for a pattern. For example, suppose we want to count the number of times the word "example" appears in a file named "example.txt". We can use the following command:
grep -c "example" example.txt

This will return the number of times the word "example" appears in "example.txt".

  1. Searching for a pattern in multiple directories We can use the -R option to search for a pattern in multiple directories. For example, suppose we want to search for the word "example" in both "dir1" and "dir2". We can use the following command:
grep -R "example" dir1 dir2

This will search for the word "example" in both "dir1" and "dir2".

  1. Using regular expressions to exclude matches We can use the -v option with regular expressions to exclude matches. For example, suppose we want to search for lines in a file named "example.txt" that do not contain the word "example". We can use the following command:
grep -v "\<example\>" example.txt

This will search for lines in "example.txt" that do not contain the word "example".

  1. Searching for a pattern in compressed files We can use the zgrep command to search for a pattern in compressed files. For example, suppose we want to search for the word "example" in a file named "example.gz". We can use the following command:
zgrep "example" example.gz

This will search for the word "example" in "example.gz".

  1. Searching for a pattern in a specific file type We can use wildcards to search for a pattern in a specific file type. For example, suppose we want to search for the word "example" in all files with the .txt extension in the current directory. We can use the following command:
grep "example" *.txt

This will search for the word "example" in all files with the .txt extension in the current directory.

  1. Searching for a pattern in a specific line range We can use the -n option to display line numbers and the -m option to specify the range of lines to search for. For example, suppose we want to search for the word "example" in lines 10 to 20 of a file named "example.txt". We can use the following command:
grep -n -m 20 "^" example.txt | tail -n +10 | head -n 11 | grep "example"

This will display lines 10 to 20 of "example.txt" and highlight any matches of the word "example".

  1. Searching for a pattern and displaying only the matched text We can use the -o option to display only the matched text. For example, suppose we want to search for all email addresses in a file named "contacts.txt". We can use the following command:
grep -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b" contacts.txt

This will search for email addresses in "contacts.txt" and display only the matched text.

  1. Searching for a pattern and displaying the file names that match We can use the -l option to display the file names that match the pattern. For example, suppose we want to search for the word "example" in all files in the current directory and display the names of the files that contain the word. We can use the following command:
egrep -l "example" *

This will search for the word "example" in all files in the current directory and display the names of the files that contain the word.

These are just a few examples of how to use the grep command in Linux. If you have further questons then you can reach out to us on whatsapp numbers: 7838238895/8909068089/8920228066