Linux 文件搜索命令Find详解
1. 文件搜索命令Find
注意:
-
服务器高峰时建议不要使用搜索命令或者搜索条件越多越好,尽量精准
-
如果包含路径与当前搜索文件名相同,可以使用转义字符或者引号:find . -name “init*”
-
使用-type d 搜索目录 f搜索文件
1.1 基本搜索功能
find [搜索范围] [匹配条件]
1. 根据文件名搜索
find . -name xxx
1.2 模糊搜索
1. *号使用
find . -name xxx* 表示以xxx开头
2. 使用?占位符
find . -name xxx??? 以xxx开头后有三个字符的
[root@localhost ~]# find . -name init??
./initit
3. linux中区分大小写 使用-iname 可以不区分
为了不区分可以使用:find . -iname xxx
1.4 固定查找大小
注意要把大小换成数据块
1. 指定大小可以使用 -n +n n
-n:小于
+n:大于
n:等于
例如:查找大于100MB的文件
# 这么写是错误的 find . -size +100
因为后后面的n大小是数据块,linux一个数据块大小是512B=0.5k
所以100MB=102400KB=204800B
find . -size +204800
1.5 根据所有者查找
find . -user shenchao
如果是根据组查找使用-group即可
1.6 根据时间属性来查询
查找当前目录下5分钟内被修改的属性的文件和目录
find . -cmin -5
参数
-amin 访问时间
-cmin change时间 更改属性,比如权限和所属等
-mmin 文件内容,文件内容修改时间
1.7 逻辑查询
find . -size +1000 -a -size -2000
当前目录下查找大于500k和 小于1000k的文件
-a:and 两个条件都满足
-o:or 两个满足一个即可
1.8 其他选项
-
根据查找结果查询其他内容
-exec /-ok
-exec:直接执行无需确认 -ok:需要确认一下 find . -name test -exec ls -ls {} \; 其中{}表示find查询的结果,\;为固定格式 ===================================== [root@localhost ~]# find . -name test -exec ls -ls {} \; 总用量 8 0 drwxr-xr-x. 2 root root 191 8月 16 12:37 bin 0 drwxr-xr-x. 3 root root 23 8月 16 12:36 lib 0 lrwxrwxrwx. 1 root root 3 8月 16 12:36 lib64 -> lib 4 -rw-r--r--. 1 root root 61 8月 16 12:37 pip-selfcheck.json 4 -rw-r--r--. 1 root root 69 8月 16 12:36 pyvenv.cfg [root@localhost ~]# find . -name test -ok ls -ls {} \; < ls ... ./test > ? y 总用量 8 0 drwxr-xr-x. 2 root root 191 8月 16 12:37 bin 0 drwxr-xr-x. 3 root root 23 8月 16 12:36 lib 0 lrwxrwxrwx. 1 root root 3 8月 16 12:36 lib64 -> lib 4 -rw-r--r--. 1 root root 61 8月 16 12:37 pip-selfcheck.json 4 -rw-r--r--. 1 root root 69 8月 16 12:36 pyvenv.cfg
-
查询类型
-type f 文件
-type d 目录
-type l 软链接文件
-
根据i节点查找,可以通过这个删除一些比较特殊的文件,比如带空格的文件,可以通过i节点删除
-inum 查询i节点
[root@localhost ~]# ls -i 8409167 anaconda-ks.cfg 13565830 lib 8409205 pybin 8409191 tet.py 12911665 init 9342880 neo4j3528.zip 1429452 test 8409172 xxx.log 341 initit 12911648 neo4j-community-3.5.28 8476141 Test.class 8409190 INITTTT 8409171 neo4j-community-3.5.28.zip 8476142 Test.java [root@localhost ~]# find . -inum 341 -ok rm -r {} \; < rm ... ./initit > ? y find: ‘./initit’: 没有那个文件或目录 [root@localhost ~]# ls -i 8409167 anaconda-ks.cfg 9342880 neo4j3528.zip 8476141 Test.class 12911665 init 12911648 neo4j-community-3.5.28 8476142 Test.java 8409190 INITTTT 8409171 neo4j-community-3.5.28.zip 8409191 tet.py 13565830 lib 8409205 pybin 8409172 xxx.log
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119257.html