find 搜索命令
Linux find命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。
## 一、语法
find [path...] [options] [expression] [action]
命令 路径 选项 表达式 动作
1.按文件名查找:
[root@hero ~]# find /etc -name "hosts" // 按名字查找
[root@hero ~]# find /etc -iname "hosts" // 不区分大小
[root@hero ~]# find /etc -iname "hos*" // 模糊查找
-i可以忽略大小写
成功输出
/etc/hosts
成功文件查找
2.按文件大小查找:
[root@hero ~]# find /etc -size +*M // 大于5M的文件
[root@hero ~]# find /etc -size *M // 5M的文件
[root@hero ~]# find /etc -size -*M // 小于5M的文件
3.指定查找的目录深度:
[root@hero ~]# find / -maxdepth 3 -a -name "ifcfg-en*" // 在3级目录下查找"ifcfg-en*"
[root@hero ~]# find / -maxdepth 4 -a -name "ifcfg-en*" // 在4级目录下查找"ifcfg-en*"
4.按文件属组、属主查找:
[root@hero ~]# find /home -user * //属主是*的文件
[root@hero ~]# find /home -group * //属组是*组的文件
5.按文件类型查找:
[root@hero ~]# find /tmp -type f // 查普通文件
[root@hero ~]# find /dev -type b //b=block
6.按文件权限查找:
[root@hero ~]# find . -perm 604 -ls //查找权限为604的文件、目录
7.找到后处理的动作 ACTIONS:
[root@hero ~]# find /etc -name "775*" -delete // 查找系统中名为755的文件和目录,删除
[root@hero ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \ // 查找到“ifcfg”后复制到/tmp