Instructions on how to use the Grep command in Linux

The Grep command stands for 'Global Regular Expression Print', which is a command in Unix/Linux used to search for lines of text in a regular expression pattern.

Instructions on how to use the Grep command in Linux Picture 1Instructions on how to use the Grep command in Linux Picture 1

The Grep command in Linux is a command to search for character strings in files and folders. How to use this command? Let's find out with  TipsMake  in the following article.

Overview of the Grep command in Linux

Grep stands for "Global Regular Expression Print", which is a command in Unix/Linux used to search for lines of text in a regular expression pattern. When you need to search for a specific string of characters in a series of files and folders, Grep will be the most useful tool, helping to shorten the search time.

What is the structure of the Grep command?

The structure of the Grep command in Linux is as follows: grep [options] pattern [FILE]

In there:

● [options]: Grep command options

● pattern: search pattern

● [FILE]: file to perform search

Some options available in the Grep command

Each option will have a different function, below are some Grep command options and their uses:

● i: Search is not case sensitive.

● v: Display lines that do not contain the search pattern.

● r or R: Search recursively in directories but R follows symbolic links

● l: Displays the names of the files containing the search pattern.

● n: Displays the number of lines matching the search pattern.

● c: Count the number of occurrences of the search pattern in each file.

● w: Search only for results that are exactly identical to the pattern.

● H: Display the file name for each result line.

● o: Show only the part that matches the search pattern.

● A [number]: Displays additional [number] lines after the result line.

● B [number]: Displays additional [number] lines before the result line.

● C [number]: Display additional [number] lines before and after the result line.

● q: Do not display any output.

● L: Displays file names that do not contain the search pattern.

● m [number]: Limit the number of search results.

● color: Color the search results

Instructions on how to use the Grep command in Linux Picture 2Instructions on how to use the Grep command in Linux Picture 2

Grep Command in Linux

Ways to use the Grep command in Linux

1. Case-insensitive search

When you need to search for a string of characters without distinguishing between upper and lower case, use the -i option of the Grep command.

grep -i "search string" file_name

To search multiple files at once, use the following command:

grep -i "search string" file_name1 file_name2

For example: grep -i "bizflycloud" example.txt. This command will search for lines containing "bizflycloud", case insensitive.

2. Show matching results

If you want to display only the lines that actually contain the search string, use the following command:

grep "search string" file_name

For example: grep "bizflycloud" example.txt. This command will search for lines containing "bizflycloud" and highlight the phrase ""bizflycloud"

3. Display file names that match the pattern

If you are working with multiple files and just want to know which file contains the search string, you can use the -l option, the command looks like this:

grep -l "search string" file_name1 file_name2 .

In addition to the -I option, you can also use –files-with-matches, which both return file names containing the search string.

For example: grep -I "bizflycloud" example1.txt example2.txt example3.txt. This command will return the names of files that contain the phrase "bizflycloud".

4. Check all words in the file

The -w option will match the entire word and filename you are looking for. Or you can use the –word-regexp option. The command is as follows:

grep -w "search term" file_name

For example: grep -w "bizflycloud" example1.txt. The above command will search for lines in the file example1.txt containing the word "bizflycloud" without any prefixes or suffixes such as "bizflycloudserver", "bizflycloudlms"

5. Search for strings in multiple files at once

To find a string in multiple files at once, you just need to use a simple command like this:

grep "search string" file_name1 file_name2 .

For example: grep "bizflycloud" example1.txt example2.txt example3.txt. The result of this command will search for lines containing "bizflycloud" in the files and display them.

6. Reverse search

To display lines that do not contain the search string, you can use the -v option with the command as follows:

grep -v "search string" file_name

For example: grep -v "bizflycloud" example.txt. This will return lines that do not contain the search string ""bizflycloud".

7. Display the row numbers of the result

If you want to know exactly which line in the file contains the search string, you can use the -n option. The command is as follows:

grep -n "search string" file_name

For example: grep -n "bizflycloud" example.txt. The line number containing the result will be displayed at the beginning of the line. and marked in a different color.

8. Limit output to certain lines

To limit the number of results displayed, you can use the -m option. The grep command in Linux to limit the number of output lines is as follows:

grep -m [number] "search string" file_name

For example: grep -m 5 "bizflycloud" example.txt. This command will only display the first 5 lines containing the string "bizflycloud".

9. Count the number of occurrences of a string in a file

If you want to know how many times the search string appears in the file, you can use the -c option. The command is as follows:

grep -c "search string" file_name

For example: grep -c "bizflycloud" example.txt. The final result is the number of times the string "bizflycloud" appears in the file example.txt.

10. Search for a string using regular expressions

To search for a string using a regular expression, use the "-E" or "-P" option. This option will enable regular expression mode.

For example, to search for a file starting with "b" using a regular expression, use the following command:

grep -E '^b' example.txt

'^b' is a regular expression to search for words starting with the letter "b'. The displayed result will be the lines in the file that have words starting with the letter "b".

Conclude

The above article has guided readers on how to use the Grep command in basic Linux. Mastering the ways to use the Grep command will help save time and increase work efficiency.

5 ★ | 1 Vote