Linux 基础命令 -- find

命令介绍

命令:find 在指定路径遍历查找文件

用法:find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path…] [expression find 路径 选项 表达式

命令选项

[root@fp-21 ~]# find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

-name	# 根据文件名中查找
-iname	# 根据文件名(不区分大小写)查找
-perm	# 根据文件权限查找
-prune	# 排除某些查找目录
-user	# 根据文件属主查找
-group	# 很具文件属组查找
-exec	# 对找到的文件执行特定的操作
-mtime	# 根据文件更改时间(天数)查找
	-num	# num 天以内修改的文件
	+num	# num 天以外修改的文件
	num		# 正好 num 天修改的文件
-mmin	# 根据文件更改时间(分钟)查找
	-num	# num 分钟以内修改的文件
	+num	# num 分钟以外修改的文件
-size	# 根据文件大小查找
	-num	# 小于 num 字节的文件
	+num	# 大于 num 字节的文件
-type	# 根据文件类型查找
	f	# 文件
	d	# 目录
	l	# 链接文件
	p	# 管道文件
	c	# 字符设备文件
	b	# 块设备文件

命令实例

# 根据文件名中查找
[root@fp-21 ~]# find /opt/ -name "aaa"
/opt/test/aaa

# 根据文件名(不区分大小写)查找
[root@fp-21 ~]# find /opt/ -iname "aaa"
/opt/test/aaa
/opt/test/AAA

# 根据文件权限查找
[root@fp-21 ~]# find /opt/ -perm 666 
/opt/test/aaa
[root@fp-21 ~]# ll /opt/test/aaa
-rw-rw-rw-. 1 root root 0 Feb 16 02:53 /opt/test/aaa

# 根据文件属主查找
[root@fp-21 ~]# find /opt/ -user tom
/opt/test/aaa
[root@fp-21 ~]# ll /opt/test/aaa 
-rw-rw-rw-. 1 tom tom 0 Feb 16 02:53 /opt/test/aaa

# 对找到的文件执行特定的操作(查找/var/log目录下,更改时间7天以上的文件,并删除)
[root@fp-21 ~]# find /var/log/ -mtime +7 | wc -l
29
[root@fp-21 ~]# find /var/log/ -mtime +7 -exec rm -rf {} \;
[root@fp-21 ~]# find /var/log/ -mtime +7 | wc -l
0

# 根据文件更改时间(分钟)查找
[root@fp-21 ~]# echo "hello world" > /opt/test/aaa
[root@fp-21 ~]# find /opt/ -mmin -1 
/opt/test/aaa

# 根据文件大小查找
[root@fp-21 ~]# ll /opt/test/aaa 
-rw-rw-rw-. 1 tom tom 12 Feb 16 21:00 /opt/test/aaa
[root@fp-21 ~]# find /opt/test/ -size -13 
/opt/test/
/opt/test/aaa
/opt/test/AAA

[root@fp-21 ~]# for i in `seq 100000`; do echo $i >> /opt/test/AAA ; done
[root@fp-21 ~]# find /opt/test/ -size +1M 
/opt/test/AAA

# 根据文件类型查找
[root@fp-21 ~]# find /opt/test/ -type f
/opt/test/aaa
/opt/test/AAA

link 查看 Linux 基础命令

只有注入思想的博客才是好的博客

发布了24 篇原创文章 · 获赞 76 · 访问量 3233

猜你喜欢

转载自blog.csdn.net/xtlhxl/article/details/104339484