grep,find

时间:2024-01-19 18:43:44

grep是强大的文本搜索工具,他可以对文件逐行查看,如果找到匹配的模式,就可以打印出包含次模式的所有行,并且支持正则表达式

find查找文件的
grep是来查找字符串的,文件的内容

grep 文件的内容 在什么目录下面

常用参数:
-c 不打印匹配的行的内容,而是打印出匹配的行数grep -c sudo /var/log/secure

-i 不区分大小写匹配 grep -i Obboot /etc/sysconfig/network-scripts/ifcfg-e*

-n 显示匹配到的行的行号 grep -n PATH ~/.bash_profile

-v 打印除匹配之外的其他行

grep name * 前面是匹配的字符串,后面是匹配的多个文件

常见正则表达式:
^匹配字符串的开头
$匹配字符串的结尾
.匹配任意的单个字符
.*任意字符
^$空白行

root@DESKTOP-BMKLFR3:/home/shell# echo '4534543gdgdfgdg' > a.sh
root@DESKTOP-BMKLFR3:/home/shell# cat a.sh
4534543gdgdfgdg
root@DESKTOP-BMKLFR3:/home/shell# grep d a.sh
4534543gdgdfgdg
root@DESKTOP-BMKLFR3:/home/shell# grep d a.sh
4534543gdgdfgdg
root@DESKTOP-BMKLFR3:/home/shell# grep -c d a.sh
1
root@DESKTOP-BMKLFR3:/home/shell# grep -i d a.sh
4534543gdgdfgdg
root@DESKTOP-BMKLFR3:/home/shell# grep -n d a.sh
1:4534543gdgdfgdg
root@DESKTOP-BMKLFR3:/home/shell# grep -v d4535 a.sh
4534543gdgdfgdg
取反拿出里面没有空白的行
root@DESKTOP-BMKLFR3:/home/shell# grep -v "^$" /etc/bash.bashrc

root@DESKTOP-BMKLFR3:/home/shell# grep -n "yun" /etc/shadow
30:yunxin_linux:$6$j5ED8vAT$Fw2DwkNx4nPR33Ph/Go/BW87WM6r1aTP6iID8fPFjSqoB0YBEkw16upmgk..rCREgokyUeD5txaAxQkHluUkb.:17791:0:99999:7:::
34:yunxin_linux2:$6$OXcW/95H$9p.l1Vec2G6H8YbxBOIBtee7xt9N13iFLV5kDAUIT1xzNp2EHY/U0iYVDyoqmG6/8XheXHjnnTQVyac6QxEfw0:17796:0:99999:7:::
35:yunxin_linux3:!:17796:0:99999:7:::
root@DESKTOP-BMKLFR3:/home/shell# grep -n "^yun" /etc/shadow
30:yunxin_linux:$6$j5ED8vAT$Fw2DwkNx4nPR33Ph/Go/BW87WM6r1aTP6iID8fPFjSqoB0YBEkw16upmgk..rCREgokyUeD5txaAxQkHluUkb.:17791:0:99999:7:::
34:yunxin_linux2:$6$OXcW/95H$9p.l1Vec2G6H8YbxBOIBtee7xt9N13iFLV5kDAUIT1xzNp2EHY/U0iYVDyoqmG6/8XheXHjnnTQVyac6QxEfw0:17796:0:99999:7:::
35:yunxin_linux3:!:17796:0:99999:7:::

grep是查找出字符串,查看文件里面的内容

find是查找出文件的:

root@DESKTOP-BMKLFR3:/home/shell# find /etc/*.s*
/etc/ld.so.cache
/etc/ld.so.conf
/etc/ld.so.conf.d
/etc/ld.so.conf.d/fakeroot-x86_64-linux-gnu.conf
/etc/ld.so.conf.d/libc.conf
/etc/ld.so.conf.d/x86_64-linux-gnu.conf

*是匹配多个字符,一个*可以匹配多个字符出来
?是匹配单个字符,一个?就匹配一个字符出来