您的位置:首页 > 其它

8、文件搜索 find

2015-09-25 12:13 211 查看

操作实例代码

[root@localhost tmp]#
[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# ls
yum.log
[root@localhost tmp]# mkdir findtest
[root@localhost tmp]# cd findtest/
[root@localhost findtest]# touch duanbang.file qinxianyou.file
[root@localhost findtest]# ls
duanbang.file  qinxianyou.file
[root@localhost findtest]# touch duanba.file qin.file du.file q.file qinxianyo.file
[root@localhost findtest]# ls -li
总用量 0
271342 -rw-r--r--. 1 root root 0 9月  25 19:35 duanba.file
271340 -rw-r--r--. 1 root root 0 9月  25 19:34 duanbang.file
271344 -rw-r--r--. 1 root root 0 9月  25 19:35 du.file
271345 -rw-r--r--. 1 root root 0 9月  25 19:35 q.file
271343 -rw-r--r--. 1 root root 0 9月  25 19:35 qin.file
271346 -rw-r--r--. 1 root root 0 9月  25 19:35 qinxianyo.file
271341 -rw-r--r--. 1 root root 0 9月  25 19:34 qinxianyou.file
[root@localhost findtest]# find -name duanbang
[root@localhost findtest]# find -name duanb*
find: 路径必须在表达式之前: duanbang.file
用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@localhost findtest]# find . -name duanbang*
./duanbang.file
[root@localhost findtest]# find . -iname DU*
./du.file
./duanba.file
./duanbang.file
[root@localhost findtest]# find . -name duanba????
[root@localhost findtest]# find . -name duanba?????
./duanba.file
[root@localhost findtest]# find . -size +2048
[root@localhost findtest]# find . -size -2048
.
./du.file
./qinxianyo.file
./duanba.file
./qin.file
./duanbang.file
./q.file
./qinxianyou.file
[root@localhost findtest]# find . -size -2048 -exec ls -il {} \;
总用量 0
271342 -rw-r--r--. 1 root root 0 9月  25 19:35 duanba.file
271340 -rw-r--r--. 1 root root 0 9月  25 19:34 duanbang.file
271344 -rw-r--r--. 1 root root 0 9月  25 19:35 du.file
271345 -rw-r--r--. 1 root root 0 9月  25 19:35 q.file
271343 -rw-r--r--. 1 root root 0 9月  25 19:35 qin.file
271346 -rw-r--r--. 1 root root 0 9月  25 19:35 qinxianyo.file
271341 -rw-r--r--. 1 root root 0 9月  25 19:34 qinxianyou.file
271344 -rw-r--r--. 1 root root 0 9月  25 19:35 ./du.file
271346 -rw-r--r--. 1 root root 0 9月  25 19:35 ./qinxianyo.file
271342 -rw-r--r--. 1 root root 0 9月  25 19:35 ./duanba.file
271343 -rw-r--r--. 1 root root 0 9月  25 19:35 ./qin.file
271340 -rw-r--r--. 1 root root 0 9月  25 19:34 ./duanbang.file
271345 -rw-r--r--. 1 root root 0 9月  25 19:35 ./q.file
271341 -rw-r--r--. 1 root root 0 9月  25 19:34 ./qinxianyou.file
[root@localhost findtest]# find . -user root -exec -ls -li {} \;
find: “-ls”: 没有那个文件或目录
find: “-ls”: 没有那个文件或目录
find: “-ls”: 没有那个文件或目录
find: “-ls”: 没有那个文件或目录
find: “-ls”: 没有那个文件或目录
find: “-ls”: 没有那个文件或目录
find: “-ls”: 没有那个文件或目录
find: “-ls”: 没有那个文件或目录
[root@localhost findtest]# whoami
root
[root@localhost findtest]# find . -user root -exec ls -li {} \;
总用量 0
271342 -rw-r--r--. 1 root root 0 9月  25 19:35 duanba.file
271340 -rw-r--r--. 1 root root 0 9月  25 19:34 duanbang.file
271344 -rw-r--r--. 1 root root 0 9月  25 19:35 du.file
271345 -rw-r--r--. 1 root root 0 9月  25 19:35 q.file
271343 -rw-r--r--. 1 root root 0 9月  25 19:35 qin.file
271346 -rw-r--r--. 1 root root 0 9月  25 19:35 qinxianyo.file
271341 -rw-r--r--. 1 root root 0 9月  25 19:34 qinxianyou.file
271344 -rw-r--r--. 1 root root 0 9月  25 19:35 ./du.file
271346 -rw-r--r--. 1 root root 0 9月  25 19:35 ./qinxianyo.file
271342 -rw-r--r--. 1 root root 0 9月  25 19:35 ./duanba.file
271343 -rw-r--r--. 1 root root 0 9月  25 19:35 ./qin.file
271340 -rw-r--r--. 1 root root 0 9月  25 19:34 ./duanbang.file
271345 -rw-r--r--. 1 root root 0 9月  25 19:35 ./q.file
271341 -rw-r--r--. 1 root root 0 9月  25 19:34 ./qinxianyou.file
[root@localhost findtest]# find .group root -exec ls -li {} \;
find: “.group”: 没有那个文件或目录
find: “root”: 没有那个文件或目录
[root@localhost findtest]# find . -group root -exec ls -li {} \;
总用量 0
271342 -rw-r--r--. 1 root root 0 9月  25 19:35 duanba.file
271340 -rw-r--r--. 1 root root 0 9月  25 19:34 duanbang.file
271344 -rw-r--r--. 1 root root 0 9月  25 19:35 du.file
271345 -rw-r--r--. 1 root root 0 9月  25 19:35 q.file
271343 -rw-r--r--. 1 root root 0 9月  25 19:35 qin.file
271346 -rw-r--r--. 1 root root 0 9月  25 19:35 qinxianyo.file
271341 -rw-r--r--. 1 root root 0 9月  25 19:34 qinxianyou.file
271344 -rw-r--r--. 1 root root 0 9月  25 19:35 ./du.file
271346 -rw-r--r--. 1 root root 0 9月  25 19:35 ./qinxianyo.file
271342 -rw-r--r--. 1 root root 0 9月  25 19:35 ./duanba.file
271343 -rw-r--r--. 1 root root 0 9月  25 19:35 ./qin.file
271340 -rw-r--r--. 1 root root 0 9月  25 19:34 ./duanbang.file
271345 -rw-r--r--. 1 root root 0 9月  25 19:35 ./q.file
271341 -rw-r--r--. 1 root root 0 9月  25 19:34 ./qinxianyou.file
[root@localhost findtest]# find . -cname -5
find: 未知的断言“-cname”
[root@localhost findtest]# find . -cmin -5
[root@localhost findtest]# find . -cmin +5
.
./du.file
./qinxianyo.file
./duanba.file
./qin.file
./duanbang.file
./q.file
./qinxianyou.file
[root@localhost findtest]# find . -amin -5
[root@localhost findtest]# find . -amin +5
.
./du.file
./qinxianyo.file
./duanba.file
./qin.file
./duanbang.file
./q.file
./qinxianyou.file
[root@localhost findtest]# find . -mmin +5
.
./du.file
./qinxianyo.file
./duanba.file
./qin.file
./duanbang.file
./q.file
./qinxianyou.file
[root@localhost findtest]# find . -mmin -5
[root@localhost findtest]# find . -mmin -5 -a -size -2048 -a -name duanbang* -exec ls -li {} \;
[root@localhost findtest]# find . -mmin +5 -a -size -2048 -a -name duanbang* -exec ls -li {} \;
271340 -rw-r--r--. 1 root root 0 9月  25 19:34 ./duanbang.file
[root@localhost findtest]# find . -mmin +5 -a -size -2048 -a -name duanbang* -o -type f -exec ls -li {} \;
271344 -rw-r--r--. 1 root root 0 9月  25 19:35 ./du.file
271346 -rw-r--r--. 1 root root 0 9月  25 19:35 ./qinxianyo.file
271342 -rw-r--r--. 1 root root 0 9月  25 19:35 ./duanba.file
271343 -rw-r--r--. 1 root root 0 9月  25 19:35 ./qin.file
271345 -rw-r--r--. 1 root root 0 9月  25 19:35 ./q.file
271341 -rw-r--r--. 1 root root 0 9月  25 19:34 ./qinxianyou.file
[root@localhost findtest]# find . -inum 271344 -exec ls -li {} \;
271344 -rw-r--r--. 1 root root 0 9月  25 19:35 ./du.file
[root@localhost findtest]# find . -inum 271344 -ok rm {} \;
< rm ... ./du.file > ? y
[root@localhost findtest]# ls -li
总用量 0
271342 -rw-r--r--. 1 root root 0 9月  25 19:35 duanba.file
271340 -rw-r--r--. 1 root root 0 9月  25 19:34 duanbang.file
271345 -rw-r--r--. 1 root root 0 9月  25 19:35 q.file
271343 -rw-r--r--. 1 root root 0 9月  25 19:35 qin.file
271346 -rw-r--r--. 1 root root 0 9月  25 19:35 qinxianyo.file
271341 -rw-r--r--. 1 root root 0 9月  25 19:34 qinxianyou.file
[root@localhost findtest]#


操作代码解释

命令作用解释
find . -name duanbang在当前目录下搜索名字为”duanbang”的文件和目录.表示范围参数 -name表示按名字搜索
find . -name duanb*在当前目录下模糊搜索名字是duanb开头的文件和目录*表示所有字符数目的未知字符 包括0
find . -iname DU*在当前目录下搜索DU开头的文件和目录-iname表示忽略大小写
find . -name duanba?????在当前目录下搜索duanba开头后面还有5个未知字符的文件和目录一个问号表示一个通配符
find . -size +2048, find / -size -2048在当前目录下查找文件大于1MB/小于1MB 的文件和目录在linux中按大小查找文件的时候,数字代表的是数据块的数量,一个数据库大小是0.5KB,所以2048=1MB
find . -size -2048 -exec ls -il {} \;查找当前目录小于1M的文件并ls 加上-li参数i参数代表ls的时候,显示文件的i节点编号,-exec [commond] {} \;是固定格式,表示对查找结果执行commond。exec可以换成ok,ok的作用是执行命令操作的时候还会询问是否确定!
find . -user root -exec ls -li {} \;查找当前目录所有者为root的所有文件并展示详情和i节点
find . -group root -exec ls -li {} \;查找当前目录所属组为root的文件并展示详情和i节点
find . -cmin -5查找属性修改时间在五分钟内的文件+5代表五分钟外,5可修改
find . -amin -5查找访问时间在五分钟内的文件+5代表五分钟外,5可修改
find . -mmin -5查找内容修改时间在五分钟内的文件+5代表五分钟外,5可修改
find . -mmin -5 -a -size -2048 -a -name duanbang* -exec ls -li {} \;查找当前目录下内容修改时间在五分钟内并且名字是duanbang开头的所有文件并且展示出详情和i节点-a 连接多个查找条件 -o 等于与 -o 也可以连接多个查找调节 等于或
find . -inum 271344 -ok rm {} \;查找出当前目录下节点为2731344的文件,并执行删除,但是删除前会询问用户是否确认删除
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: