linux如何用locate和find命令查找文件?
admin
2023-02-22 05:20:05
0次

1 locate 查找文件 使用updatedb 建立mlocate.db 整个磁盘文件搜索建立索引 影响磁盘性能。

locate -i 不区分大小写
locate -n 只列出前n个
locate -r " .(jpg|png)$"   查找以jpg或png结尾的文件  支持j基本的正则表达式
2 find 实时搜索 默认递归搜索
  find  [OPTION] ...[查找路径] [查找条件][处理动作]
/etc/passwd  ----  maxdepth 指定搜索层级 本例 /etc/下1层目录中搜索passwd.
find /etc/ -maxdepth 2 -mindepth 2 -name filename 搜索最多到第二层 最少搜索第二层只搜索第二层
[23:54:08 root@localhost ~]$find /data -depth  先列出文件再列出文件夹 排列有变化
/data/scripts37/diskcheck1026.sh
/data/scripts37/creatuseradd.sh
/data/scripts37/read23.sh
。。。。。。。。。。。
/data/f1.txt
/data/dir/f1
/data/dir
/data/f2
/data
-iname "文件名称":不区分大小写
[00:02:27 root@localhost data]$ll -i
total 68
100663360 drwxr-xr-x. 2 root root    16 Feb 16 23:51 dir
69 -rw-r--r--. 1 root root     0 Feb 16 20:51 f1.txt
85 -rw-r--r--. 1 root root   595 Feb 16 23:51 f2
68 drwxr-xr-x. 2 root root  4096 Feb 16 23:13 scripts37
67 -rw-r--r--. 1 root root 61440 Feb 16 08:17 scripts37.tar
33554496 drwxr-xr-x. 3 root root    19 Feb 16 15:28 test
[00:02:33 root@localhost data]$find /data/ -inum 70
/data/scripts37/diskcheck1026.sh
[00:06:25 root@localhost data]$ll -i
total 72
100663360 drwxr-xr-x. 2 root root    16 Feb 16 23:51 dir
69 -rw-r--r--. 1 root root     0 Feb 16 20:51 f1.txt
85 -rw-r--r--. 2 root root   595 Feb 16 23:51 f2
85 -rw-r--r--. 2 root root   595 Feb 16 23:51 f2.link
68 drwxr-xr-x. 2 root root  4096 Feb 16 23:13 scripts37
67 -rw-r--r--. 1 root root 61440 Feb 16 08:17 scripts37.tar
33554496 drwxr-xr-x. 3 root root    19 Feb 16 15:28 test
[00:06:35 root@localhost data]$find /data/ -samefile /data/f2
/data/f2
/data/f2.link
[00:07:08 root@localhost data]$
— regex "PSTTERN" :以PATTERN匹配整个文件路径。而非文件名称
00:10:50 root@localhost data]$find /usr/share/pixmaps/ -regex ".*.jpg$"  ---匹配完成路径
find /etc -type d 搜索、etc下的文件夹
find 、dev -type b 搜索块文件
   find /data -empty -ls   搜索空文件 大写为零
   [00:22:44 root@localhost data]$find /data/ -empty -ls
69    0 -rw-r--r--   1 root     root            0 Feb 16 20:51 /data/f1.txt
   find /data  ! -empty -ls 搜索非空文件                !逻辑取反
       find /data  -not  -empty -ls 搜索非空文件        
与 :-a
或 : -o
非: -not !
#find /data  ! -empty -ls -----查非空文件及文件夹
#find /data  -not  -empty -ls -----查非空文件及文件夹
find /data -name "f"  -a -type f 查找以f开头并且是普通文件 -a 可省略 默认就是并且模式
find /data -name "f

find /data -name "f" -o -type f 查找以f开头或者是普通文件的文件
#find /etc -path '/etc/sane.d'  -a -prune -o -name ".conf"  --- -a并且 -prune剪切  剪切etc/sane.d/目录后去搜索 /etc下的  .conf 文件。
*find  /etc (  -path "/etc/sane.d" -o "/etc/fonts"  ) -a -prune -o -name ".conf" 搜索 /etc下 除了、etc/sane.d文件夹及、etc/fonts文件夹里的以 .conf结尾的文件**
10240+0 records in
10240+0 records out
10240 bytes (10 kB) copied, 0.140448 s, 72.9 kB/s
[03:23:35 root@localhost data]$ll f1
-rw-r--r--. 1 root root 10240 Feb 17 03:23 f1
[03:23:41 root@localhost data]$find -size 10k
./f1
[03:24:06 root@localhost data]$
#find /etc/  -szie -6k{查找0-5K之间的文件}
#find /etc/ -size +6k {查找大于6k的文件}
find -atime(默认是以天为单位) +10 10天以前的文件
find -atime 10 查找第十天至第十一天之间的文件 不写路径默认当前路径
find -perm 644 查找当前644的文件权限精确匹配
[04:17:32 root@localhost data]$find -perm -222
[04:17:49 root@localhost data]$ll
total 80
drwxr-xr-x. 2 root root    16 Feb 16 23:51 dir
drwxr-xr-x. 2 root root     6 Feb 17 00:23 dir2
----------. 1 root root 10240 Feb 17 03:23 f1
-rw-r--r--. 1 root root     0 Feb 16 20:51 f1.txt
-rw-r--r--. 1 root root   595 Feb 16 23:51 f2
lrwxrwxrwx. 1 root root     8 Feb 17 00:08 f2.link -> /data/f2
等价命令
[04:38:24 root@localhost data]$find -perm -001 > /data/ls.log
[04:42:48 root@localhost data]$find -perm /001 -ok chmod o+w {} \;  查找文件 并做权限修改  "{}"获取查找结果 -ok 必须以 "\;"结束 语法要求
< chmod ... ./scripts37 > ? y
< chmod ... ./scripts37/diskcheck1026.sh > ? y
< chmod ... ./scripts37/creatuseradd.sh > ? y
< chmod ... ./scripts37/age.sh > ? y
< chmod ... ./scripts37/yesorno_case23.sh > ? y
< chmod ... ./scripts37/count.sh > ? y
< chmod ... ./scripts37/YESORNO23.sh > ? y
< chmod ... ./scripts37/systeminfo23.sh > ? y
< chmod ... ./scripts37/yesorno23.sh > ? y
< chmod ... ./scripts37/checkdisk.sh > ? y
< chmod ... ./scripts37/useDISK.sh > ? y
< chmod ... ./scripts37/set.sh > ? y
< chmod ... ./test > ? y
< chmod ... ./test/test1 > ? y
< chmod ... ./dir > ? y
< chmod ... ./dir2 > ? y
< chmod ... ./fid > ? y
drwxr-xrwx. 2 root root    16 Feb 16 23:51 dir
drwxr-xrwx. 2 root root     6 Feb 17 00:23 dir2
-rw-r--r--. 1 root root     0 Feb 16 20:51 f1.txt
drwxr-xrwx. 2 root root     6 Feb 17 00:35 fid
-rw-r--r--. 1 root root  1540 Feb 17 04:38 flog2.log
-rw-r--r--. 1 root root   325 Feb 17 04:42 ls.log
drwxr-xrwx. 2 root root  4096 Feb 16 23:13 scripts37
-rw-r--r--. 1 root root 61440 Feb 16 08:17 scripts37.tar
drwxr-xrwx. 3 root root    19 Feb 16 15:28 test
-rw-------. 1 root root 28M Dec 14 08:31 /boot/initramfs-3.10.0-957.el7.x86_64.img
-rw-------. 1 root root 11M Dec 14 08:32 /boot/initramfs-3.10.0-957.el7.x86_64kdump.img

find  /etc  -name passwd ---find +l路径 + 指定文件名

[23:42:14 root@localhost scripts37]$find /etc/ -maxdepth 1 -name passwd

/data/scripts37.tar

-name 支持通配符  如: -name "*.jpg"

-inum n 按照inode号查找

-samefile name 相同的inode号的文件

-link n 链接数为n的文件

[00:13:17 root@localhost data]$find / -user zhonghua  -ls  --按用户搜索zhonghua的文件 ls把文件属性列出。

find / -nouser  ----查找无主文件

67160128    0 drwxr-xr-x   2 root     root            6 Feb 16 15:28 /data/test/test1

支持逻辑条件

find /data ( -name "f*" -o -type f ) -a ls -- -a默认是有的 逻辑与优先级高 为了不出错 用括号把优先执行的命令括起来先执行 需要转义。

~# find /etc  -name "*.conf" ---""避免出错

#排除搜索  

[03:22:12 root@localhost data]$dd if=/dev/zero of=f1 bs=1 count=10240  --创建文件f1 大写10K bs=1 (1个字节) 1024字节是1K ,10240字节是10k

#find /etc/ -size 6k { 查找5到6K之间大写的文件}

#find -atime -10 10天以内的文件

[04:14:25 root@localhost data]$find /data/scripts37 -perm /622 -ls  模糊匹配 /622 是所有者所属组与other 只要有一个匹配就查出来。

./f2.link                                      ------------------ -222 中“”-“”意思是且 所有者 所属组 和other都要有写权限

[04:37:54 root@localhost data]$find -perm -001 -fls /data/flog2.log

< chmod ... . > ? y

[04:48:55 root@localhost data]$find -perm /002 -exec ls -l {} \;  exec代替OK 不再提示直接出结果

total 72

[04:52:44 root@localhost data]$find / -size +10M -exec ls -lh {} \;

-rw-------. 1 root root 71M Dec 14 08:29 /boot/initramfs-0-rescue-c8f47886da6d4ec687a38cc0be7425b1.img

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...