文件查找与压缩 locate find

1 locate 查找文件 使用updatedb 建立mlocate.db 整个磁盘文件搜索建立索引 影响磁盘性能。
locate -i 不区分大小写
locate -n 只列出前n个
locate -r " .(jpg|png)$" 查找以jpg或png结尾的文件 支持j基本的正则表达式

2 find 实时搜索 默认递归搜索
   find  [OPTION] ...[查找路径] [查找条件][处理动作]

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

[23:42:14 root@localhost scripts37]$find /etc/ -maxdepth 1 -name passwd
/etc/passwd ---- maxdepth 指定搜索层级 本例 /etc/下1层目录中搜索passwd.

find /etc/ -maxdepth 2 -mindepth 2 -name filename 搜索最多到第二层 最少搜索第二层只搜索第二层

[23:54:08 root@localhost ~]$find /data -depth  先列出文件再列出文件夹 排列有变化

/data/scripts37.tar
/data/scripts37/diskcheck1026.sh
/data/scripts37/creatuseradd.sh
/data/scripts37/read23.sh
。。。。。。。。。。。
/data/f1.txt
/data/dir/f1
/data/dir
/data/f2
/data

-name 支持通配符 如: -name "*.jpg"
-iname "文件名称":不区分大小写

-inum n 按照inode号查找
[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

-samefile name 相同的inode号的文件
[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]$

-link n 链接数为n的文件
— regex "PSTTERN" :以PATTERN匹配整个文件路径。而非文件名称
00:10:50 root@localhost data]$find /usr/share/pixmaps/ -regex ".*.jpg$" ---匹配完成路径

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

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

find /etc -type d 搜索、etc下的文件夹
find 、dev -type b 搜索块文件
    find /data -empty -ls   搜索空文件 大写为零
    [00:22:44 root@localhost data]$find /data/ -empty -ls

67160128 0 drwxr-xr-x 2 root root 6 Feb 16 15:28 /data/test/test1
69 0 -rw-r--r-- 1 root root 0 Feb 16 20:51 /data/f1.txt

扫描二维码关注公众号,回复: 9260802 查看本文章
    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
" -o -type f 查找以f开头或者是普通文件的文件

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

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

#排除搜索
#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结尾的文件**

[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
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/ -size 6k { 查找5到6K之间大写的文件}
#find /etc/ -szie -6k{查找0-5K之间的文件}
#find /etc/ -size +6k {查找大于6k的文件}

find -atime(默认是以天为单位) +10 10天以前的文件

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

find -atime 10 查找第十天至第十一天之间的文件 不写路径默认当前路径

find -perm 644 查找当前644的文件权限精确匹配

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

[04:17:32 root@localhost data]$find -perm -222

./f2.link ------------------ -222 中“”-“”意思是且 所有者 所属组 和other都要有写权限
[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:37:54 root@localhost data]$find -perm -001 -fls /data/flog2.log

[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 ... . > ? y
< 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

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

total 72
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

[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
-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

猜你喜欢

转载自blog.51cto.com/12246080/2472063