find是个使用频率比较高的命令。常常用它在系统特定目录下,查找具有某种特征【名字类型属主权限等】的文件。
path:要查找的目录路径。
~ 表示$HOME目录. 表示当前目录/ 表示根目录
-print :表示将结果输出到标准输出
-exec :对匹配的文件执行该参数所给出的shell命令。形式为 command {} \; ,注意{}与\; 之间有空格
-ok :与-exec作用相同,区别在于,在执行命令之前,都会给出提示,让用户确认是否执行
options常用的有下选项:-name 按照名字查找-perm 安装权限查找-prune 不再当前指定的目录下查找-user 文件属主来查找-group 所属组来查找-nogroup 查找无有效所属组的文件-nouser 查找无有效属主的文件-type 按照file.html' target='_blank'>文件类型查找
下面通过一些简单的例子来介绍下find的常规用法:
1、按名字查找
在当前目录及子目录中,查找大写字母开头的txt文件
[root@linuxso.com ~]# find . -name '[A-Z]*.txt' -print
[root@linuxso.com ~]# find /etc -name 'host*' -print
在当前目录及子目录中,查找不是out开头的txt文件[root@linuxso.com ~]# find ~ -name '*' -print
[root@linuxso.com ~]# find . -perm 755 -print
在当前目录及子目录下,查找符号链接文件
[root@linuxso.com .code]# find . -type l -print
查找属主是www的文件
[root@linuxso.com .code]# find / -user www -type f -print
[root@linuxso.com .code]# find / -nouser -type f -print
[root@linuxso.com .code]# find / -group mysql -type f -print
[root@linuxso.com .code]# find / -nogroup -type f -print
查找2天内被更改过的文件
[root@linuxso.com .code]# find . -mtime -2 -type f -print
[root@linuxso.com .code]# find . -mtime +2 -type f -print
[root@linuxso.com .code]# find . -atime -1 -type f -print
[root@linuxso.com .code]# find . -atime +1 -type f -print
[root@linuxso.com .code]# find . -ctime -1 -type f -print
[root@linuxso.com .code]# find . -ctime +1 -type f -print
[root@linuxso.com .code]# find . -cmin +10 -type f -print
查找比aa.txt新的文件
[root@linuxso.com .code]# find . -newer "aa.txt" -type f -print
[root@linuxso.com .code]# find . ! -newer "aa.txt" -type f -print
[root@linuxso.com .code]# find . -newer 'aa.txt' ! -newer 'bb.txt' -type f -print
查找超过1M的文件
[root@linuxso.com .code]# find / -size +1M -type f -print
[root@linuxso.com .code]# find . -size 6c -print
[root@linuxso.com .code]# find . -size -32k -print
查找del.txt并删除,删除前提示确认
[root@linuxso.com .code]# find . -name 'del.txt' -ok rm {} \;[root@linuxso.com .code]# find . -name 'aa.txt' -exec cp {} {}.bak \;