25、文件查找命令

网友投稿 615 2022-08-30

25、文件查找命令

25、文件查找命令

在文件系统上查找符合条件的文件,可以使用locate、find命令,locate非实时查找,find采用实时查找

locate

locate查找需要先创建索引数据库(/var/lib/mlocate.db) 索引创建需要消耗系统资源,建议在闲时执行(updatedb),默认情况下每天会执行一次

locate工作特点

因使用数据库查找,查找速度快

模糊查找

非实时查找

搜索文件全路径,不仅仅是文件名

只能搜索用户具备读取和执行权限的目录

格式  locate  [option]...[pattern]...

常用选项

-i  不区分大小写

-n N 只列出N个匹配

-r 使用级别正则表达式

find

find是实时查找工具,通过遍历指定路径来完成文件查找工作

特点:慢、实时、精确

格式:find [option]...[path默认当前路径] [条件] [执行动作]

option选项

-P Never follow symbolic links. This is the default behaviour. When find examines or prints information a file, and the file is a symbolic link, the information used shall be taken from the properties of the symbolic link itself. -L Follow symbolic links. When find examines or prints information about files, the information used shall be taken from the properties of the file to which the link points, not from the link itself (unless it is a broken symbolic link or find is unable to examine the file to which the link points). Use of this option implies -noleaf. If you later use the -P option, -noleaf will still be in effect. If -L is in effect and find discovers a symbolic link to a subdirectory during its search, the subdirectory pointed to by the symbolic link will be searched. When the -L option is in effect, the -type predicate will always match against the type of the file that a symbolic link points to rather than the link itself (unless the symbolic link is broken). Using -L causes the -lname and -ilname predi? cates always to return false. -H Do not follow symbolic links, except while processing the command line arguments. When find examines or prints information about files, the information used shall be taken from the properties of the symbolic link itself. The only exception to this behaviour is when a file specified on the command line is a symbolic link, and the link can be resolved. For that sit? uation, the information used is taken from whatever the link points to (that is, the link is followed). The information about the link itself is used as a fallback if the file pointed to by the symbolic link cannot be examined. If -H is in effect and one of the paths specified on the command line is a symbolic link to a directory, the contents of that directory will be examined (though of course -maxdepth 0 would prevent this).

条件选项

-maxdepth 最大搜索目录深度 (指定目录下的文件为第一级)

-mindepth 最小搜索目录深度

-depth 或-d  对每个目录先处理目录内的文件,再处理目录本身(本身目录在最下面)

-name "文件名称“:支持glob,如* ? [] [^] ,通配符需要使用双引号

-iname “文件名称”:忽略大小写

-inum n 按照inode好查找

-samefile name  相同inode号的文件

-links n 按照链接数查找

-regex “pattern”:已pattern匹配整个文件路径查找,非文件名

-user username 查找属主为username(uid)的文件

-group groupname 查找属组为groupname(gid)的文件

-uid userid 查找属主为uid的文件

-gid groupid 查找属组为gid的文件

-nouser:查找没有属主的文件  删除的用户没有删除家目录及邮件

-nogroup:查找没有属组的文件

-type [f|d|l|s|b|c|p] 文件类型查找文件,f普通文件 d目录文件 l 符号链接文件

-empty 空目录或文件

组合条件

-a 且  -o 或者    -not或者!:非  优先级  ! -a  -o

!A -o !B 等同于  !(A -a B)

!A -a !B 等同于 !(A -o B)

排除目录方式

-path “path” -a -prune 排除path目录

文件大小

-size [+|-] num单位

3k|M|G:(3-1,3)k|M|G

-3k|M|G:(0,3-1)k|M|G

+3k|M|G :(3,无穷)k|M|G

按照时间

按照天为单位

-atime|ctime|mtime  [+|-]num  访问时间、创建时间、修改时间

3:{3,3+1)

+3:(3+1,无穷)

-3:(0,3)

-amin|cmin|mmin 按照分钟

根据权限查找

-perm [/|-]MODE

MODE:精确匹配

/MODE:任何一个匹配就行,ugo必须包含权限就算匹配

-MODE:每一个都必须要有这个权限 ugo必须包含权限就算匹配

处理动作

-print 打印,默认设置

-ls:类似 ls -l

-fls file :输出到file文件

-delete: 删除

-ok command {} \; 对查找到的每个文件执行command命令,执行命令前需要交互式确认 \;结束

-exec command {} \; 等同上方,无需确认 \;结束

{}:用于引用查找文件名称自己

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:轻量级运维工具pssh
下一篇:图解kubernetes容器探活机制核心实现(kubernetes支持哪些探测机制)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~