SoFunction
Updated on 2025-03-09

Detailed explanation of Linux grep instructions (practical)

The grep instruction in Linux is as powerful as the find instruction. Find mainly searches for files, while grep is the content. The two complement each other. I took advantage of the weekend and summarized it carefully!

1. Function
The grep command in Linux system is a powerful text search tool. It can search text using regular expressions and print matching lines. grep is Global Regular Expression Print, which represents the global regular expression version, and its usage permissions are for all users.

2. Main parameters
[options] Main parameters:
-c: Only output the count 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 output file names containing matching characters.
-n: Show matching rows and line numbers.
-s: No error message showing non-existent or no matching text.
-v: Shows all lines that do not contain matching text.

Main parameters of pattern regular expression:
\: Ignore the original meaning of special characters in regular expressions. Escape
^: Match the start line of the regular expression.
$: Match the end line of the regular expression.
\<: Start with the line matching the regular expression.
\>: End of the line matching the regular expression.
[ ]: Single character, such as [A], that is, A meets the requirements.
[ - ]: Range, such as [A-Z], that is, A, B, C and Z meet the requirements.
. : All single characters.
*: There are characters, and the length can be 0.

Simple example of using commands
Note: It is best to use double quotes/ when entering the string to search, and when using regular expressions when using pattern matching, pay attention to using single quotes.
$ grep ‘test’ d* -r  <==> grep test d* -r  i.e. 'test' <==> test
Shows all files starting with d that contain test lines.
$ grep ‘test' aa bb cc -r
Shows the line matching test in aa, bb, cc files.
$ grep ‘[a-z]\{5\}' aa
Displays all lines containing at least 5 consecutive lowercase characters per string.
By default, 'grep' only searches for the current directory. If there are many subdirectories under this directory, explicitly require searching for subdirectories:grep -r

4. Give an example more (mainly copying others, standing on the shoulders of experts)
# more   

b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
a013386
b044525
m8987131
B081016
M45678
B103303
BADc2345

# more | grep '[a-b]'//Scope: For example, [A-Z], that is, A, B, C and Z meet the requirements, that is, print lines with a-b - ignore case (maybe system dependencies)
b124230
b034325
a081016
a022021
a061048
b103303
a013386
b044525
# more | grep '[a-b]'*//The result is the same as the previous instruction-test result
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
a013386
b044525
m8987131
B081016
M45678
B103303
BADc2345

# more | grep 'b'//Single character; if [A], that is, A meets the requirements, outputs a line with b
b124230
b034325
b103303
b044525
# more | grep '[bB]'//Output lines with B or b
b124230
b034325
b103303
b044525
B081016
B103303
BADc2345

# grep 'root' /etc/group//Output line with root in /etc/group directory
root::0:root
bin::2:root,bin,daemon
sys::3:root,bin,sys,adm
adm::4:root,adm,daemon
uucp::5:root,uucp
mail::6:root
tty::7:root,tty,adm
lp::8:root,lp,adm
nuucp::9:root,nuucp
daemon::12:root,daemon
# grep '^root' /etc/group//^: Match the start line of the regular expression --> the line starting with root
root::0:root
# grep 'root$' /etc/group//$: Match the end line of the regular expression --> the line ending with root
root::0:root
mail::6:root

# more | grep -i 'b1..*3'// -i: Ignore case
b124230
b103303
B103303

# more | grep -iv 'b1..*3'//-v: Find rows that do not contain matches
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
a013386
b044525
m8987131
B081016
M45678
BADc2345

# more | grep -in 'b1..*3'//-n: Show matching rows and line numbers.
1:b124230
9:b103303
15:B103303

# more
the test file
their are files
The end

# grep 'the'  
the test file
their are files

# grep '\<the' // \<Rules start
the test file
their are files

# grep 'the\>' //The end of the line \>
the test file

# grep '\<the\>' //I don’t know how to express it, haha.
the test file

# grep '\<[Tt]he\>'  //And output lines with The or the
the test file
The end

# grep '[239].' //Output all rows that start with 2, 3 or 9 and are two numbers

# grep '^[^the]' //The row of the line does not match is the row of the line

grep -E 'The|test' //Show lines containing The or test

Note: All of the above have been tested by me. It can be said that running on my computer meets the standards.