1. The grep command under Linux
1.1 Introduction:
The grep (Global Regular Expression Print) command is a powerful text search tool that searches text using regular expressions . and print out the matching lines. Indicates the global regular expression output, and its usage permission is all users.
1.2 Format
grep [options]
1.3 Main parameters
[options] main parameters
-r
: Search subdirectories-d
: Do not search subdirectories-c
: output only the count of matching rows-C
: Matched contexts display [number] lines respectively-i
: case insensitive (only for single characters)-h
: Do not display the file name when querying multiple files-l
: When querying multiple files, only output the file names containing matching characters-L
: List unmatched filenames-w
: only match the whole word, such as hello does not match helloabc-n
: Display matching lines and line numbers-s
: Do not display error messages for non-existent or no-matching text-v
: show all lines that do not contain matching text--color=auto
: Display of the found keywords with color
pattern regular expression main parameters
\
: Ignore the original meaning of special characters in regular expressions^
: start line matching regular expression$
: Matches the end of the regular expression line\<
: start from the line matching the regular expression\>
: end of line from matching regular expression[]
: A single character, such as [A], that is, A meets the requirements[-]
: Range, such as [AZ], that is, A to Z all meet the requirements.
: all single characters*
: All characters, the length can be 0
1.4 grep example description
# grep 递归检索指定扩展名的文件内容
grep -rn --include=*.后缀名 "检索词"
# grep 排除指定文件类型查找文件内容
grep "检索词" -rR --exclude=*.{
后缀名1,后缀名2}
# grep 排除指定目录查找文件内容
grep "检索词" -rn --exclude-dir={
目录1,目录2}
# 显示所有以d开头的文件中包含 test的行
grep "test" d*
# 显示在aa,bb,cc文件中包含test的行
grep "test" aa bb cc
# 显示所有包含每行字符串至少有5个连续小写字符的字符串的行
grep "[a-z]\{5\}" aa
# 显示/usr/src目录下的文件(不含子目录)包含magic的行
grep magic /usr/src
# 显示/usr/src目录下的文件(包含子目录)包含magic的行
grep -r magic /usr/src
# 只匹配整个单词,而不是字符串的一部分(如匹配’magic’,而不是’magical’)
grep -w pattern files
# :在文件名 包含202005的文件中查找hello字符串,并返回该文件。(不加 -l参数会返回行内容)
grep -rl hello *202005*
# 在a,b,c三个文件中查找hello,并返回该文件。
grep -rl "hello" a.txt b.txt c.txt
# 查找包含4个及以上数字的 行返回。(注意:\表示启用正则含义,或者用 egrep "[0-9]{4}" a.txt)
grep "[0-9]\{4\}" a.txt
# 当匹配多个行,可以通过管道转到 less 上查看。
grep hello /usr/file/* | less
# 显示既匹配 pattern1 又匹配 pattern2 的行。
grep pattern1 a.txt| grep pattern2
# 匹配正则pattern1 或pattern2 的行。
grep pattern1 | pattern2 a.txt
# 会匹配 "helloa"、"hellob"、"yhelloa"等。
grep hello*
# 匹配"hello"和"helloa",但不是"yhello"。
grep "\<hello" *
# 只匹配"hello",而不是"yhello"或"helloa"等其他的字符串。
grep "\<hello\>"
2. Find command under Linux
When using linux, it is often necessary to search for files. Among them, the commands to search mainly include find and grep. But there is a difference between the two:
- The find command searches based on the attributes of the file, such as file name, file size, owner, all groups, whether it is empty, access time, modification time, etc.
- grep searches based on the content of the file, and matches each line of the file according to a given pattern (patter).
2.1 Basic format
find path expression
2.2 Search by file name
# 在根目录下查找文件httpd.conf,表示在整个硬盘查找
find / -name httpd.conf
# 在/etc目录下文件httpd.conf
find /etc -name httpd.conf
# 使用通配符*(0或者任意多个)。表示在/etc目录下查找文件名中含有字符串‘srm’的文件
find /etc -name "*srm*"
# 表示当前目录下查找文件名开头是字符串‘srm’的文件
find . -name "srm*"
2.3 Search according to file characteristics
# 查找在系统中最后10分钟访问的文件(access time)
find / -amin -10
# 查找在系统中最后48小时访问的文件
find / -atime -2
# 查找在系统中为空的文件或者文件夹
find / -empty
# 查找在系统中属于 group为cat的文件
find / -group cat
# 查找在系统中最后5分钟里修改过的文件(modify time)
find / -mmin -5
#查找在系统中最后24小时里修改过的文件
find / -mtime -1
#查找在系统中属于fred这个用户的文件
find / -user fred
#查找出大于10000000字节的文件(c:字节,w:双字,k:KB,M:MB,G:GB)
find / -size +10000c
#查找出小于1000KB的文件
find / -size -1000k
2.4 Find files using hybrid search methods
The parameters are: ! , -and(-a), -or(-o).
# 在/tmp目录下查找大于10000字节并在最后2分钟内修改的文件
find /tmp -size +10000c -and -mtime +2
# 在/目录下查找用户是fred或者george的文件文件
find / -user fred -or -user george
# 在/tmp目录中查找所有不属于panda用户的文件
find /tmp ! -user panda