Home > Article > Operation and Maintenance > Comprehensive explanation of the usage of grep command under Linux
To learn the Linux system, you must master the grep command. The grep command is used to find matching lines in files or standard output. Its power is that it supports regular expressions. In daily work, grep is definitely one of the most used commands.
Note: This article only introduces the usage of grep, and does not introduce regular expressions.
Let’s take a look at some of its common options and their meanings:
Options
# 加上-i选项,忽略大小写 # grep -i timezone /usr/local/php/etc/php.ini ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = PRCgrep supports searching in multiple files
# 这里我们加上-n选项,输出文件的行号 # grep -in stdio itoa.c quicksort.c itoa.c:2:#include <stdio.h> quicksort.c:1:#include <stdio.h>The reverse selection -v is used. When I run a program and want to see if the program still exists in the process, I can use the following method
# ps aux | grep curl.php root 14374 98.3 1.2 277844 12396 pts/0 R+ 07:54 1:07 php curl.php root 14404 0.0 0.0 112664 984 pts/2 R+ 07:55 0:00 grep --color=auto curl.phpThere is a problem here, we You need to use -v to filter out the process of grep itself
# ps aux | grep curl.php | grep -v grep root 14374 98.5 1.2 277844 12396 pts/0 R+ 07:54 2:36 php curl.phpWe want to count the number of lines containing root in the last command, so we need to use the -c option here.
# last | grep -c root 353There is often a need to replace certain text in files in batches, so how do you know which files these texts are in. grep can do it, with the -l option
# find . -type f -exec grep -l define {} \; ./find.c ./itoa2.c ./wc.c ./test.c ./wordcnt.c ./longestline.c ./cal.c ./sortline2.c ./sortline.c ./found.c ./atof.cLet’s look at another example. This example uses the -E extended regular rule. We want to filter out the comment lines and blank lines in the php.ini file
grep -vE '^;|^$' /usr/local/php/etc/php.iniThere is also a commonly used option -r, which can be used to recursively search all files in the current directory and subdirectory files. Next, we want to check what footer files are in the website directory. We can use the following command to find
grep -rn footer ./
The above is the detailed content of Comprehensive explanation of the usage of grep command under Linux. For more information, please follow other related articles on the PHP Chinese website!