Linux命令grep,find使用

1、grep

在文件中查找内容

[root@bigdata01 ~]# grep root /etc/passwd    --查找/etc/passwd文件下包含root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@bigdata01 ~]# grep -n root /etc/passwd   --标出行号
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@bigdata01 ~]# grep -n -i ROOT /etc/passwd   --不区分大小写并标出行号
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
[root@bigdata01 ~]# grep -v -i ROOT /etc/passwd   --取反,即不包含root的行

-- 可以与正则一起使用
[root@bigdata01 ~]# grep -n '^root' /etc/passwd   --是root开头的
1:root:x:0:0:root:/root:/bin/bash
[root@bigdata01 ~]# grep -n -v '^root' /etc/passwd  --不是root开头的

练习一下:

在/var/log/secure中过滤出'Failed'; 

grep -n 'Failed' /var/log/secure

2、find

 

#语法格式
find 目录 指定类型 指定名字

1.在/etc/目录中找出文件名叫hostname文件
[root@bigdata01 ~]# find /etc -name hosts 
/etc/hosts

2.在找出/etc/下面以.conf结尾的文件
[root@bigdata01 ~]# find /etc -name *.conf

大于 使用+加号 -size +10k 大于10k文件
小于 使用-减号 -size -10k 小于10k文件

3.根据大小找出文件 在/etc/目录下面找出大于10kb的文件
[root@bigdata01 ~]# find /etc/ -size +10k
查找出来的内容如何验证是否正确呢?
[root@bigdata01 ~]# du -h /etc/aliases.db
12K     /etc/aliases.db

4.找出/etc/目录下以.conf结尾的,最近7天的文件
[root@bigdata01 ~]# find /etc/ -name '*.conf' -mtime -7
/etc/resolv.conf
/etc/mongod.conf
[root@bigdata01 ~]# stat /etc/resolv.conf     --stat 文件名  可以查看这个文件的所有详细信息
  文件:"/etc/resolv.conf"
  大小:76              块:8          IO 块:4096   普通文件
设备:fd00h/64768d      Inode:33554505    硬链接:1
权限:(0644/-rw-r--r--)  Uid:(    0/    root)   Gid:(    0/    root)
最近访问:2024-09-12 08:06:38.127701693 +0800
最近更改:2024-09-12 08:06:37.701704036 +0800
最近改动:2024-09-12 08:06:37.701704036 +0800
创建时间:-

 由以上 时间轴可以看出,最右边为当前时,+5 代表大于等于 6 天前的文件名, -5 代表小于等于 5 天内的文件名, 5 则是代表 5-6 那一天的文件名。

6.找出/etc/中以.conf结尾大于10kb修改时间是7天之前的文件
[root@bigdata01 ~]# find /etc/ -name '*.conf' -size +10k -mtime +7
/etc/lvm/lvm.conf

7.查找文件的时候指定最多找多少层目录
-maxdepth 1 选项位置第1个,指定find命令查找的最大深度,不加上就是所有层。
[root@localhost ~]# find / -maxdepth 2  -name "*.conf"
 /etc/resolv.conf
 /etc/libaudit.conf
 /etc/dracut.conf

注意:
 -maxdepth 属性不能在 name 之后,否则报错!!!

8.结合``符号案例,目标将所有查询的文件以列表形式显示,类似于ll
[root@bigdata01 ~]# ls -l `find /etc -maxdepth 2 -name *.conf -mtime +7 -size +10k`
-rw-r--r--. 1 root root 94435 4月  11 2018 /etc/lvm/lvm.conf

``里面是命令,等于$() ,这里不能用管道符,不然查出来的结果是错误的。

#解决方案:xargs 参数传递,主要让一些不支持管道的命令可以使用管道技术
find /etc -maxdepth 2 -name *.conf -mtime +7 -size +10k | xargs ls -l

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值