SoFunction
Updated on 2025-03-10

Detailed explanation of grep command in shell

Search for text files with 'grep'

If you are looking for a string in several text files, you can use the 'grep' command. 'grep' searches the text for the specified string. For example: Suppose you are searching for a file with the string 'magic' in the '/usr/src/linux/Documentation' directory:

$ grep magic /usr/src/linux/Documentation/*
:* How do I enable the magic SysRQ key?
:* How do I use the magic SysRQ key?

Where the file ‘' contains the string, discussing the functionality of SysRQ.

By default, 'grep' only searches for the current directory. If there are many subdirectories under this directory, 'grep' will be listed in the following form:

grep: sound: Is a directory

This may make the output of 'grep' difficult to read. Here are two solutions:

Clearly require searching for subdirectories:grep -r

Ignore subdirectories:grep -d skip

Of course, if you expect a lot of output, you can go through the pipeline

Go to 'less' to read:

$ grep magic /usr/src/Linux/Documentation/* | less

This way, you can read more easily.

One thing to note is that you must provide a file filtering method (use * if you search for all files). If you forget, ‘grep’ will wait until the program is interrupted. If you encounter this situation, press ctrl+c and try again. (important!!)

Here are some interesting command line parameters:

grep -i pattern files: Search case-insensitively. By default, case sensitive.
grep -l pattern files: list only matching file names,
grep -L pattern files: lists mismatched file names,
grep -w pattern files: match only the entire word, not part of the string (such as matching 'magic', not 'magical'),
grep -C number pattern files: The matching context displays the [number] rows respectively,
grep pattern1 | pattern2 files: displays rows matching pattern1 or pattern2,
grep pattern1 files | grep pattern2: Shows rows that match both pattern1 and pattern2.

Here are some special symbols for searching:

\< and \> mark the beginning and end of the word respectively.

For example:

grep man * Will match ‘Batman'、‘manic'、‘man'wait, 
 grep '\&lt;man' * Match 'manic'and‘man', but not 'Batman', 
grep '\&lt;man\&gt;' Match only‘man', not 'Batman'or‘manic'etc.。 

'^': refers to the matching string at the beginning of the line.

'$': refers to the matching string at the end of the line.

If you are not used to command line parameters, you can try 'grep' in the graphical interface, such as reXgrep. This software provides syntax such as AND, OR, NOT, etc., and also has beautiful buttons :-). If you just need a clearer output, try fungrep .

Common command syntax for Grep

1. Double quotes and single quotes

When entering string parameters in the g re e p command, it is best to enclose them in double quotes. For example: "m y s t r i n g". There are two reasons for this. One is to prevent it from being misunderstood as the s h e l l command, and the other is to find strings composed of multiple words, such as: "jet plane". If it is not enclosed with double quotes, then the word p l a n e

It will be mistaken for a file, and the query result will return an error message that "the file does not exist".

When calling variables, double quotes should also be used, such as: g r e p”$M Y VA R” file name, if not, no result will be returned.

When calling pattern matching, single quotes should be used. [root@mypc]# echo `grep 123 `  (#Note that it is reverse single quotes)

2. Commonly used g re e p options are:

-c   Only output counts of matching rows.
-i   Case insensitive (only for single characters).
-h   The file name is not displayed when querying multiple files.
-l   When querying multiple files, only the file name containing matching characters is output.
-n   Show matching rows and line numbers.
-s   No error message showing non-existent or no matching text.
-v   Show all lines that do not contain matching text.

3. Special – Query in multiple files

$ grep “sort”*.doc   ( #Find the string "so r t" in the current directory) $ grep “sort it” *   (#Or query words in all files”sort it”)

All the following examples refer to querying in a single file

4. Row Match

$ grep -c “48″ 
$ 4           (#g r e p returns the number 4, meaning that there are 4 lines containing the string "4 8".  )$ grep “48″      (#Show contains”4 8″String4Line text)

5. Display all rows that satisfy the matching pattern:

[root@mypc oid2000]# grep -n 1234 
1:1234
3:1234ab

6. Exact match

[root@mypc oid2000]# grep “1234\>” 
1234

7. Query blank lines, query lines that start or end with a certain condition.

Use ^ and $ to query blank lines. Use the -n parameter to display the actual number of rows

[root@mypc oid2000]# grep -n "^$" (return result 2: # indicates that the second line is an empty line)[root@mypc oid2000]# grep -n "^abc" (#Query rows starting with abc)[root@mypc oid2000]# grep -n “abc$”  (#Query withabcThe ending line)

8. Match special characters and query characters with special meanings, such as $. ‘ ” * [] ^ | \ + ? , \ must be added before the specific characters.

[root@mypc oid2000]# grep "\." (#Query all lines containing ".")[root@mypc oid2000]# grep “my\.conf”  (#Query file namemy. c o n fThe way)

9. Directory query

[root@mypc oid2000]# ls -l |grep "^d" (#If you want to query the directory in the directory list)[root@mypc oid2000]# ls -l |grep "^d[d]" (#Query all files that do not contain directories in one directory)[root@mypc]# ls -l |grpe “^d…..x..x” (#Query the directory collections that other users and user group members have executable permissions)

The above is the grep command in the shell introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time!