centos find grep

一、find

1.-name 按照文件名搜索

  完全匹配 或 *代表任意字符 或 ?代表一个任意字符 或 [abc...]表示里面的一个字符

[root@localhost etc]# find /etc/ -name "centos*"
/etc/centos-release
/etc/centos-release-upstream
/etc/libreport/events.d/centos_report_event.conf
/etc/lvm/archive/centos_00000-947003905.vg
/etc/lvm/backup/centos

[root@localhost etc]# find -name centos_00000-947003905.vg
./lvm/archive/centos_00000-947003905.vg

2.-iname 不区分大小写

[root@localhost etc]# find /etc/ -iname "Centos*"
/etc/yum.repos.d/CentOS-Base.repo
/etc/yum.repos.d/CentOS-CR.repo
/etc/yum.repos.d/CentOS-Debuginfo.repo
/etc/yum.repos.d/CentOS-Media.repo
/etc/yum.repos.d/CentOS-Sources.repo
/etc/yum.repos.d/CentOS-Vault.repo
/etc/yum.repos.d/CentOS-fasttrack.repo
/etc/centos-release
/etc/centos-release-upstream
/etc/libreport/events.d/centos_report_event.conf
/etc/lvm/archive/centos_00000-947003905.vg
/etc/lvm/backup/centos

二、grep

grep 是一个最初用于Unix操作系统的命令行工具。

grep -[acinv] '搜索内容串' filename

常用参数选项 -c 计算找到的符合行的次数。 -i 忽略大小写。 -n 显示匹配行及行号。 -v 找到没有搜索字符串的行。 -h 查询多文件时不显示文件名。 -l 查询多文件时只输出包含匹配字符的文件名。 -s 不显示不存在或无匹配文本的错误信息。
实际使用举例:

1、搜索日志,查询有多少条503错误

grep -c '503' /var/log/httpd/error_log-20141116

2、搜索含有 error 字样的行,并且输出行号

grep -n 'error' /var/log/httpd/error_log-20141116

3、搜索没有 error 字样的行,并且输出行号

grep -nv 'error' /var/log/httpd/error_log-20141116

4、搜索寻找安装的软件版本有几个

#centos下查看安装的Python版本
rpm -qa | grep -i python
#Ubuntu下查看安装的Python版本
sudo dpkg -l | grep -i python

5、过滤配置文件的注释符号#

#匹配 # 符号的行,但是输出的是 # 符号以外的行
grep -v '#' /etc/httpd/conf/httpd.conf

6、查询每个网卡和IP地址

ifconfig | grep -n inet

 7、忽略大小写搜索(-i)

grep -i "ErroR" log.txt

8、所有子目录下的搜索(-r)

 grep -r "exception" log.txt

 9、精准全匹配搜索(-w)

grep -w "boo" /path/to/file

10、精准全字匹配搜索两个不同单词

grep -w 'word1|word2' /path/to/file

 11、统计字符串出现的次数(-c)

grep -c 'word' /path/to/file
#-n的话, 会在结果中,列出匹配字符串的序列号,并且会列出内容
grep -n 'word' /path/to/file

 12、只列出文件名(-l)

grep -l 'main' *.pls

13、高亮显示搜索结果(–color)

grep --color apache /etc/passwd

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值