find命令
一、find语法
find:文件在指定的目录及其子目录下查找指定的文件。
语法:find 目录 [选项] [动作]
二、常用选项
-name filename #查找名为filename的文件
-perm #按执行权限来查找
-user username #按文件属主来查找
-group groupname #按组来查找
-mtime -n +n #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime -n +n #按文件访问时间来查,-n指n天以内,+n指n天以前
-ctime -n +n #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
-size n[c] #查长度为n块[或n字节]的文件
-type b/d/c/p/l/f #查是块设备、目录、字符设备、管道、符号链接、普通文件
-depth #使查找在进入子目录前先行查找完本目录
-newer f1 !f2 #查更改时间比f1新但比f2旧的文件
三、动作
-exec: 对匹配的文件执行该参数所给出的shell命令。命令形式为'command' { } \;,注意{ }和\;之间的空格。
-ok: 和-exec的作用相同,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
四、实例
[root@host-39-108-217-12 ~]# find . -name a.txt -exec cp {} /opt \;
[root@host-39-108-217-12 ~]# ls /opt
a.txt
说明:{} 可以理解为find找到的文件
-name:按名称查找
[root@host-39-108-217-12 ~]# find /etc -name yum.conf
/etc/yum.conf
-perm:按文件权限查找
[root@host-47-98-97-124 ~]# find /etc -perm 777
/etc/chk_mysql.sh
-user:按文件属主查找
[root@host-39-108-217-12 ~]# find /etc/zabbix/ -user zabbix
/etc/zabbix/zabbix_agentd.conf
-mtime,-atime,-ctime
find /etc -mtime -5 查找修改5天以内时间的文件
find /etc -atime -5 查找访问5天以内时间的文件
fine /etc -ctime -5 查找变更5天以内时间的文件
find /etc -mtime +5 查找5天前修改过的文件
find /etc -atime +5 查找5天前访问过的文件
fine /etc -ctime +5 查找5天前变更过的文件
atmie:accesstime 访问时间 mtime:modifytime 修改文件内容 ctime:changetime 修改文件属性
-size:按文件大小查找,单位可以是b(block)、c(byte)、w(two-byte)、k、M、G
在/dev目录下查找大于1M的文件
find /dev -size +1M
在/dev目录下查找小于1M的文件
find /dev -size -1M
在/dev目录下查找等于1M的文件
find /dev -size 1M
小结: 数据块(block)=512字节=0.5K,1KB=1024Byte,1MB=1024X1024Byte,1GB=1024X1024X1024Byte
-type:按文件类型查找文件
f 普通文件
l 符号链接
d 目录,查找某一个目录文件
c 字符设备
b 块设备
s 套接字
查看某一个目录文件
[root@host-47-98-97-124 ~]# find / -type d -name zhang
/root/zhang
/opt/zhang
查找某一个文件
[root@host-47-98-97-124 ~]# find / -type f -a -name chk_mysql.sh
/usr/local/zabbix/scripts/chk_mysql.sh
总结:-a 和 -o表示与和或的关系,通常是指多个选项之间的关系,比如-type f -a -name chk_mysql.sh之间的关系。