find查找文件和grep用法

半世苍凉 提交于 2019-11-27 08:30:24

• 根据预设的条件递归查找对应的文件
– find [目录] [条件1] [-a|-o] [条件2] …
– 常用条件表示:
-type 类型(f、d、l)
-name “文档名称”
-size +|-文件大小(k、M、G)
-user 用户名
-mtime 根据文件修改时间
################################################

-type 类型(f文件、d目录、l快捷方式)
-name '文档名称'

[root@server0 ~]# find /boot/ -type d
[root@server0 ~]# find /boot/ -type f
[root@server0 ~]# find /boot/ -type l
[root@server0 ~]# ls /boot/grub/menu.lst
[root@server0 ~]# ls -l /boot/grub/menu.lst

[root@server0 ~]# find /etc/ -name ‘passwd’
[root@server0 ~]# find /etc/ -name ‘tab’
[root@server0 ~]# find /etc/ -name ‘tab
[root@server0 ~]# find /etc/ -name '
.conf’

[root@server0 ~]# mkdir /root/nsd01
[root@server0 ~]# mkdir /root/nsd02
[root@server0 ~]# touch /root/nsd03.txt
[root@server0 ~]# find /root/ -name ‘nsd*’

]# find /root/ -name ‘nsd*’ -a -type f

]# find /root/ -name ‘nsd*’ -type f
]# find /root/ -name ‘nsd*’ -type d

################################################
-size +或-文件大小(k、M、G)
-user 用户名 #按照数据的所有者进行查找

[root@server0 ~]# find /boot/ -size +10M #大于10M的
[root@server0 ~]# find /boot/ -size -10M
[root@server0 ~]# find /boot/ -size +300k

/proc:不占用磁盘空间,占用内存空间

[root@server0 ~]# find / -user student
[root@server0 ~]# find /home -user student

#################################################
-mtime 根据文件修改时间
-mtime +10 #10天之前的数据
-mtime -10 #最近10天之内的数据

[root@server0 ~]# find /root/ -mtime +1000
[root@server0 ~]# find /root/ -mtime +10
[root@server0 ~]# find /root/ -mtime -2

三个月以前:
[root@server0 ~]# find /root/ -mtime +90

#################################################

find扩展使用
• 使用find命令的 -exec 操作
– find … … -exec 处理命令 {} ;
– 优势:以 {} 代替每一个结果,逐个处理,遇 ; 结束

]# find /boot/ -size +10M
]# find /boot/ -size +10M -exec cp {} /opt ;
]# ls /opt/

]# find /root/ -name ‘nsd*’
]# find /root/ -name ‘nsd*’ -exec rm -rf {} ;
]# find /root/ -name ‘nsd*’

案例4:查找并处理文件
• 使用find命令完成以下任务
– 找出所有用户 student 拥有的文件
– 把它们拷贝到 /root/findfiles/ 文件夹中

[root@server0 ~]# mkdir /root/findfiles
[root@server0 ~]# find / -user student -type f

]# find / -user student -type f -exec cp {} /root/findfiles/ ;

]# ls -A /root/findfiles/

################################################
grep用法
显示文件的有效信息(去除注释行,去除空行)
]# grep -v ^# /etc/login.defs
]# grep -v ^# /etc/login.defs | grep -v ^$

]# grep -v ^# /etc/login.defs | grep -v ^$ > /opt/1.txt
]# less /opt/1.txt

在Linux大多数配置文件中,以#开头的行为注释行

^$ : 匹配空行
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!