linux中,常用的find命令:
1:查询当前所有的.txt文件
[root@localhost ~]# find ./ -name "*.txt"
./.cache/tracker/db-version.txt
./.cache/tracker/db-locale.txt
./.cache/tracker/miner-applications-locale.txt
./.cache/tracker/last-crawl.txt
./.cache/tracker/first-index.txt
./1/2.txt
./1/1.txt
./1.txt
2:删除一天以内修改过的文件(和删除一天前的文件类似)
[root@localhost 1]# touch 10.txt
[root@localhost 1]#
[root@localhost 1]# ls
10.txt 1.txt 2.txt nginx-1.12.2 wenjian
[root@localhost 1]# find ./ -mtime -1
./
./10.txt
[root@localhost 1]# find ./ -mtime -1 -exec rm -rf {} \;
rm: refusing to remove "." or ".." directory: skipping "./"
[root@localhost 1]# ls
1.txt 2.txt nginx-1.12.2 wenjian
find ./ -mtime -1 是指一天内;当然 +10就是十天之前,而10就是第十天创建的文件或者目录文件
-exec +命令 {} \;固定写法
"{}"表示前面的内容,也就是“find ./ -mtime -1”得到的内容,
例如:
将本文件下的.txt文件拷贝到/root/find/文件下面
[root@localhost 1]# find ./ -name "*.txt" -exec cp {} /root/find/ \;
[root@localhost 1]# ls /root/find/
1.txt 2.txt
3:查询多个类型的文件
-a and 并且 -o or 或者
+ 超过 - 低于
查询本文件下所有的.pdf和.sh文件
[root@localhost 1]# find ./ -name "*.pdf" -o -name "*.sh"
./1.pdf
./2.sh
查询/etc文件下大于30k并且小于40k的文件
[root@localhost 1]# find /etc/ -size +30k -a -size -40k| wc -l
4
4:权限查询
查询本文件下权限是777的文件
[root@localhost 1]# find ./ -perm 777
./1
查询本文件不低于644权限的文件
[root@localhost 1]# find ./ -perm -644
...
5:查询层序
查询第一层的文件
[root@localhost 1]# find /bin/ -maxdepth 1 -perm 777
/bin/gtbl
/bin/bashbug
/bin/gtar
/bin/sh
/bin/bunzip2
...
6:查询所属用户
查询属于ad用户的文件
[root@localhost 1]# find / -user ad