find命令用于查找文件系用的指定文件
在学习这些内容的时候老师给过一张关于find命令格式的图解
个人觉得特别好,所以在这里分享一下
个人觉得特别好,所以在这里分享一下
1.按名字查找
例:查找大写字母开头的文件
find . -name '[A-Z]*.txt' -print #这里的*表示除了换行符意外的零到多个字符
在/etc及其子目录中,查找host开头的文件
find /etc -name 'host*' -print
find /etc -name 'host*' -print
在$HOME目录及其子目录中查找所有文件
find ~ -name '*' -print
2.按目录查找
在当前目录除aa之外的子目录内搜索txt文件
find . -path “./aa" -prune -o -name "*.txt" -print
在当前目录除aa之外的子目录内搜索txt文件
find . -path “./aa" -prune -o -name "*.txt" -print
这里的-path的作用是指定一个目录 -prune的意思是将该目录除外的意思
-o 是一个逻辑判断符号,整个语句的逻辑是这样的
find命令查找文件时,是将该目录和其子文件的目录中的文件一个个的进行遍历查找
如果该文件是./aa目录下的,就将其除外不进行任何操作,如果该文件不是./aa目录下的,而且是以.txt结束的文件,就将其输出
-o 是一个逻辑判断符号,整个语句的逻辑是这样的
find命令查找文件时,是将该目录和其子文件的目录中的文件一个个的进行遍历查找
如果该文件是./aa目录下的,就将其除外不进行任何操作,如果该文件不是./aa目录下的,而且是以.txt结束的文件,就将其输出
在当前目录及除aa和bb之外的子目录中查找txt文件
find . \(-path "./aa" -o -path "./bb"\) -prune -o -name "*.txt" -print
find . \(-path "./aa" -o -path "./bb"\) -prune -o -name "*.txt" -print
3.按照权限查找
在当前目录及子目录中,查找属主具有读写执行,其他具有读权限的文件
在当前目录及子目录中,查找属主具有读写执行,其他具有读权限的文件
find . -perm 755 -print
4.按类型查找
在当前目录及子目录下,查找符号链接文件
在当前目录及子目录下,查找符号链接文件
find . -type I -print
5.按照属主和属组进行查找
查找属主是www的文件
5.按照属主和属组进行查找
查找属主是www的文件
find / -user www -type f -print # f代表普通文件
查找属主被删除的文件
find . -nouser -type f -print
find . -nouser -type f -print
查找属组mysql的文件
find . -group mysql -type f -print
find . -group mysql -type f -print
查找被用户组删除的文件
find / -nogroup -type f -print
find / -nogroup -type f -print
6.按照时间查找
查找2天内被更改过的文件
find -mtime -2 -type f -print
find -mtime -2 -type f -print
查找2天前辈更改过的文件
find . -mtime +2 -type f -print
find . -mtime +2 -type f -print
查找一天内被访问过的文件
find . -atime -1 -type f -print
find . -atime -1 -type f -print
查找一天前被访问过的文件
find . -atime +1 -type f -print
find . -atime +1 -type f -print
查找一天内状态被改变的文件
find . -ctime -1 -type f -print
find . -ctime -1 -type f -print
查找一天前状态被改变的文件
find . -ctime +1 -type f -print
查找10分钟以前状态被改变的文件
find . -cmin +10 -type f -print
find . -cmin +10 -type f -print
按照文件新旧进行查找(我自己没有试验过,总结的时候看到的。。)
查找比chao.txt文件新的文件
find . -newer "chao.txt" -type f -print
find . -newer "chao.txt" -type f -print
查找不比chao.txt文件新的文件
find . ! -newer "chao.txt" -type f -print
查找比chao.txt新,比xiu.txt文件旧的文件
find . -newer 'chao.txt' ! -newer 'xiu.txt' -type f -print
find . -newer 'chao.txt' ! -newer 'xiu.txt' -type f -print
按照大小查找
查找超过1M的文件
$ find / -size +1M -type f -print
查找超过1M的文件
$ find / -size +1M -type f -print
查找等于6字节的文件
$ find . -size 6c -print
$ find . -size 6c -print
查找小于32k的文件
$ find . -size -32k -print
$ find . -size -32k -print
执行命令
查找del.txt并删除,删除前提示
$ find . -name 'del.txt' -ok rm {}\;
查找del.txt并删除,删除前提示
$ find . -name 'del.txt' -ok rm {}\;
查找chao.txt并备份为chao.txt.bak
$ find . -name 'chao.txt' -exec cp {} {}.bak \;