find命令

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之间的关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东城绝神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值