Linux文件查找相关知识
(一)简介
which :命令查找
find: 文件查找,针对文件名;
locate:文件查找,依赖数据库
find: 文件查找,针对文件名;locate:文件查找,依赖数据库
(二)命令查找
查找ls 命令的位置
#which ls //从PATH环境变量 which 直接跟命令会显示其绝对路径有别名时会显示其别名(alias)
标题 whereis ls 和which差不多
(三)命令文件查找
find
1 语法
find [path…] -[options] [expression] [action]
命令 路径 选项 表达式(描述 ) 动作
2 按文件名找
[root@tianyun ~]# find /etc -name “hosts”
[root@tianyun ~]# find /etc -iname “HOSTS” //-i忽略大小写
[root@tianyun ~]# find /etc -iname “hos*” //只要含有hos就会找出来
alias别名系统使用(类似创建命令)
alias xxx=‘ 命令内容’
xxx:你想要的随意字符
-h人性化单位
3 按文件大小
[root@tianyun ~]# find /etc -size +5M //大于5M
[root@tianyun ~]# find /etc -size 5M //等于5M
[root@tianyun ~]# find /etc -size -5M //小于5M
[root@tianyun ~]# find /etc -size +5M -ls //-ls找到的处理动作
4指定查找的目录深度:
-maxdepth levels
-mindepth levels
[root@tianyun ~]# find / -maxdepth 3 -a -name “ifcfg-en*”
[root@tianyun ~]# find / -maxdepth 4 -a -name “ifcfg-en*”
数字是几级
-a是and 和的意思
5按文件属主、属组找:
[root@tianyun ~]# find /home -user jack //属主是jack的文件
[root@tianyun ~]# find /home -group hr //属组是hr组的文件
[root@tianyun ~]# find /home -nouser //缺失UID的文件
6按文件类型(了解)
[root@tianyun ~]# find /dev -type f //f普通
[root@tianyun ~]# find /dev -type d //d目录
[root@tianyun ~]# find /dev -type l //l链接
[root@tianyun ~]# find /dev -type b //b块设备
[root@tianyun ~]# find /dev -type c //c字符设备
[root@tianyun ~]# find /dev -type s //s套接字
[root@tianyun ~]# find /dev -type p //p管道文件
7 按文件权限:
普通权限、
[root@tianyun ~]# find . -perm 644 -ls //-ls是动作之一 精确权限
[root@tianyun ~]# find . -perm -644 -ls //包含权限即可
8找到后处理的动作 ACTIONS:
(1)类型
-print 打印,默认选项
-ls
-delete
-exec 后面跟自定义的shell命令
-ok 后面跟自定义的shell命令
(2)实例
find /etc -name “ifcfg*”
#find /etc -name “ifcfg*” -print
#find /etc -name “ifcfg*” -ls
find /etc -name “775*” -delete /775.txt是自定义文件
find /etc -name “ifcfg*” -exec cp -rvf {} /tmp ; //不提示
find /etc -name “775*” -exec rm -rf {} ;
find /etc -name “ifcfg*” -ok cp -rvf {} /tmp ; //提示 复制
-ok连接符 {}代表前面找到的文件是源文件 \结束符