目录
1.grep: 文件内容过滤
[root@qfedu.com ~]# grep 'root' /etc/passwd #从/etc/passwd文件中过滤root字段
[root@qfedu.com ~]# grep -r 'root' /etc/ #递归查找
2.查找命令
[root@qfedu.com ~]# which ls
3.查询命令和配置文件的位置
[root@qfedu.com ~]# whereis cp
[root@qfedu.com ~]# whereis passwd
4.find
语法:
# find 路径 条件 跟条件相关的操作符 [-exec 动作]
默认不写路径时查找的是当前路径
条件:
1.指定的名称 -name
2.文件类型 -type
3.权限 -perm
4.时间 (atime,mtime,ctime) #天
4.1按文件名查找 -name
从根开始找文件
[root@qfedu.com ~]# find / -name “*file2” #从根开始找文件
[root@qfedu.com ~]# find /etc -iname "Ifcfg-ens33" #-i忽略大小写
-----------取反 分别找出test5 和除了test5的文件 ------------
[root@qfedu.com ~]# find /home/ -name *test5*
[root@qfedu.com ~]# find /home/ ! -name "test5*" # !--取反
4.2按文件大小 -size
[root@qfedu.com ~]# find /etc -size +5M #大于5M
[root@qfedu.com ~]# find /etc -size 5M #等于5M
[root@qfedu.com ~]# find /etc -size -5M #小于5M
-a:and
[root@qfedu.com ~]# find / -size +3M -a -size -5M #查找/下面大于3M而且小于5M的文件
-o:or
[root@qfedu.com ~]# find / -size -1M -o -size +80M #查找/下面小于1M或者大于80M的文件
[root@qfedu.com ~]# find / -size -3M -a -name "*.txt" #查找/ 下面小于3M而且名字是.txt的文件
4.3按时间查找 (atime,mtime,ctime)
-atime = access访问时间
-mtime = modify改变时间 内容修改时间会改变
-ctime = change修改时间 属性修改时间会改变
-amin #分钟
-mmin
-cmin
[root@qfedu.com ~]# find /opt -mtime +5 #修改时间5天之前
[root@qfedu.com ~]# find . -mtime -2 #修改时间2天之内
[root@qfedu.com ~]# find /opt -mmin -2 #修改时间在2分钟之内
4.4按文件类型 -type
f:普通文件 d:目录 l:软链接 b:设备
[root@qfedu.com ~]# find /dev -type f #f普通文件
[root@qfedu.com ~]# find / -type f -size -1M -o -name "*.txt"
4.5按文件权限 -perm
[root@qfedu.com ~]# find . -perm 644 #.是当前目录 精确查找644
[root@qfedu.com ~]# find /usr/bin -perm -4000 #包含set uid
5.找到后处理动作
[root@qfedu.com ~]# find /etc -name "ifcfg*" -exec cp -rf {} /tmp \;
#exec命令对之前查找出来的文件做进一步操作----- 查找带ifcfg开头的文件复制到tmp下
[root@qfedu.com ~]# find /home/ -name test* -exec rm -rf {} \;
#{}为前面查找到的内容,\; 格式
[root@qfedu.com ~]# # find /home/ -name "test*" | xargs -i cp {} /tmp/
#找到之后删除处理xargs 参数传递