• 欢迎访问显哥博客,本网站纯属学习技术,绝无商业用途,欢迎小伙伴们共同学习!研究技术!QQ:52249909 加我QQ
  • 世界75亿人,这么小的概率,能认识你,是我一生的幸运,不妨加个QQ接触一下:52249909 加我QQ

find命令的详解【显哥出品,必为精品】

运维基础 lixian 4年前 (2019-12-20) 20043次浏览 1个评论 扫描二维码
文章目录[隐藏]

1、find命令概述

文件查找:文件名、文件的大小、文件的类型、文件的属主属组、文件的层级查找、文件的权限查找、文件修改时间

语法:find 路径 选项 表达式 动作
find /etc -name “lixian” -print

2、find命令的选项

-name选项:

-name #根据名称查找
-iname #查找的时候忽略大小写

[root@lixian ~]# touch lixian
[root@lixian ~]# touch LIXIAN
[root@lixian ~]# find /root -name "lixian"           #根据文件名精确查找
/root/lixian
[root@lixian ~]# find  /root   -name  "*lx*"          #匹配文件名中包含lx的字符串
/root/lixlxian
[root@lixian ~]# find /root -iname "lixian"          #查找忽略大小写
/root/lixian
/root/LIXIAN
[root@lixian ~]# find /root -name "lx?"               #匹配以lx开头,并后面匹配任意一个字符
/root/lx1
/root/lx2
/root/lx3
/root/lx4
[root@lixian ~]# find /root -name "lx[1-4]"          #匹配中括号中连续的字符
/root/lx1
/root/lx2
/root/lx3
/root/lx4
[root@lixian ~]# find /root -name "lx[158]"         #匹配中括号中的任意一个字符
/root/lx8
/root/lx1
/root/lx5
[root@lixian ~]# find /root -name "lx[^4]"           #排除lx后面加一个字符,但不是4的字符的文件
/root/lx1
/root/lx2
/root/lx3
[root@lixian ~]# find /root ! -name "lixian"          #排除lixian这个文件
/root
/root/.lesshst
/root/.bashrc
/root/lx6zhenhsuai
/root/.viminfo
/root/.cshrc

-type选项:
f #普通文件
d #目录
l #软链接文件
b #快设备文件
c #字符设备
s #套接字文件,socket文件
p #管道文件

find /etc -type f -ls           #查找类型为普通文件

#根据文件的层级查找

-maxdepth 正整数

find  /etc/  -maxdepth 1   -type  f  #查找etc一层目录下的普通文件

#根据属主属组查找

-user
-group
-nouser
-nogroup

find  /home  -user  tom  -ls		#查找属主是tom用户
find  /home  -group apps  -ls		#查找属组是apps
find  /home  -user yw1 -group yw -ls	#查找属主是yw1并且属组是yw
 find  /home  -user yw1 -a  -group yw -ls	##查找属主是yw1并且属组是yw   -a  并且,不写也是并且
find  /home   \( -user yw1 -o  -group apps \)   -ls  #查找属主是yw1或者属组是apps  -o  或者,必须要用小括号将将条件括起来。
find  /home/  -nouser  -ls  #查找没有属主 
find  /home/  -nogroup  -ls		#查找没有属组的
find  /home  -nouser  -a -nogroup  -ls		#查找没有属主并且没有属组的
find  /home \( -nouser  -o  -nogroup \)  -ls  #查找没有属主或者没有属组的

#文件的大小

-size b k M G

find  /var/log/  -size  -1b  -ls		              #查找空文件
find  /var/log   -size  -100k  |xargs  ls -lh	      #查找小于100k的文件
find  /var/log   -size  22k  |xargs  ls -lh	      #查找等于22k的文件
find  /var/log   -size  +500k  |xargs  ls -lh       #大于500k的文件
find  /var/log  -size  +2M  |xargs  ls -lh           #大于2M
find  ./  -size   -3M  |xargs ls -lhd                    #小于3M
find  ./  -size   2M  |xargs ls -lhd                     #等于2M  ,b单位和M单位会凑整,四舍五入

find  /var/log/  -size  +100k  -a -size -300k  |xargs  ls -lh       #并且
find  /var/log/  \( -size  +1M  -o -size -1b \)  |xargs  ls -lh       #或者
find  /var/log/ !  \( -size  +1k  -a -size -10M \) |xargs  ls -lhd  #取反

#文件的权限

-perm

find  ./    -perm  644  -ls			#精确查找644权限的文件 
find  ./    -perm  -644  -ls			#查找权限中包含644权限,必须满足所有条件
find  /  -type f   -perm  -222  -ls	#满足所有权限位都有可写权限
find  ./  -type f   -perm  /222  -ls	#满足其中一个条件就可以
find  / -perm  -4000 -ls		#查找拥有setuid的权限
find  / -perm  -2000 -ls		#查找拥有setgid的权限
find  / -perm  -1000 -ls		#查找拥有sticky的权限

#根据文件的最后的修改时间查找

-mtime

find  ./  -mtime  +7  -ls             #查找最后修改时间七天以前的,不包含第七天,不会计算当天的文件
find  ./  -mtime  7  -ls 		#显示第七天的文件
find  ./  -mtime  -7  -ls 	        #显示7天以内的文件,不包含第7七天的文件,包含当天的文件
find  ./  -mtime  -7  -mtime +3   -ls 		#查找3天以前并且7天以内的文件

3、find命令动作

-print #默认动作,将查找出来的文件打印出来
-ls #以长格式显示查找出来的文件的详细信息,包含inode号
-delete #把查找出来的文件删除,只能删除空目录

find  ./  -mtime +7  -delete
find  ./  -type  d  -delete

-ok #后面可以自定义shell命令,会交互式询问你,

find  ./  -size  +1M  -ok ls -lhd  {} \;

-exec #后面可以自定义shell命令,不会交互式的询问,会将所有查找出来的文件进行一个个的赋值给后面的{},
; #是shell命令的分隔符,\取消其特殊含义

find  ./  -size  +1M  -exec   ls -lhd  {} \;

4、find命令跟xargs的配合使用

find  ./  -type  f  |xargs  ls -lh
find  ./  -type  f  |xargs  cp  -t  /opt/
find  ./  -type  f  |xargs -I {}  cp {}  /mnt

-I #将find命令查找出来的文件进行统一赋值给{},后续的命令进行调用该{}.

find  ./  -type  f  |xargs -I {}  mv  {}  /mnt
find  /root  -maxdepth  2  -type  d  |xargs -I {}   du  -sh {}

本站博主 , 版权所有丨如未注明 , 均为原创
转载请注明原文链接:find命令的详解【显哥出品,必为精品】
喜欢 (52)

您必须 登录 才能发表评论!

(1)个小伙伴在吐槽
  1. Reading your article helped me a lot and I agree with you. But I still have some doubts, can you clarify for me? I'll keep an eye out for your answers.
    egame2023-06-17 04:27