find命令的学习

1.按照文件名查找文件
find /etc  -name  passwd   # 查找/etc/目录下的passwd 文件
-maxdepth  2  # 最多查找2层
-mindepth  1   # 最少查找1层

 [root@server mnt]# find /etc/ -name passwd  # 查找/etc下的passwd 文件
 /etc/passwd
 /etc/pam.d/passwd
 [root@server ~]# find /etc/  -maxdepth 1 -name passwd  # 最多只查找一层
 /etc/passwd
 [root@server ~]# find /etc/  -mindepth 2 -name passwd   # 最少要查找两层
 /etc/pam.d/passwd 
2.按照文件的用户和组来查找文件
find  /mnt  -user   root    # 查找/mnt目录下文件用户是root的文件
find  /mnt  -group  root    # 查找/mnt目录下文件用户组是root的文件
find  /mnt  -user student -group  root    # 查找/mnt目录下文件用户是student并且用户组是root的文件
find  /mnt  -not -group  root    # 查找/mnt目录下文件用户组不是root的文件
find  /mnt  -not -user student -o  -group  root    # 查找/mnt目录下文件用户不是student或者用户组是root的文件,-o表示或者 

[root@server ~]# touch /mnt/file{1..5}
[root@server ~]# chown student.student /mnt/file1
[root@server ~]# chown westos.student /mnt/file2
[root@server ~]# chown root.westos /mnt/file3
[root@server ~]# chown root.student /mnt/file4
[root@server ~]# cd /mnt[root@server mnt]# ll

在这里插入图片描述

[root@server ~]# find /mnt -user root
/mnt
/mnt/file3
/mnt/file4
/mnt/file5
[root@server ~]# find /mnt -group  student
/mnt/file1
/mnt/file2
/mnt/file4
[root@server ~]# find /mnt -user root -group  student
/mnt/file4
[root@server ~]# find /mnt -not -user root -o -group  student
/mnt/file1
/mnt/file2
/mnt/file4
[root@server ~]# find /mnt -user westos -o -not -group student
/mnt
/mnt/file2
/mnt/file3
/mnt/file5 
3.按照文件大小查找文件
find  /mnt  -size  10k             # 查找/mnt下大小等于10k的文件
find  /mnt  -size  -10k            # 查找/mnt下大小小于10K的文件
find  /mnt  -size  +10k           # 查找/mnt下大小大于10k的文件 

[root@server ~]# dd if=/dev/zero of=/mnt/file1 bs=1 count=10240  # 截取指定大小的文件
[root@server ~]# dd if=/dev/zero of=/mnt/file2 bs=1 count=20480
[root@server ~]# dd if=/dev/zero of=/mnt/file3 bs=1 count=30960
[root@server ~]# du -sh /mnt/file1       # 查看文件大小
12K	/mnt/file1
[root@server ~]# du -sh /mnt/file2
20K	/mnt/file2
[root@server ~]# du -sh /mnt/file3
32K	/mnt/file3
[root@server ~]# du -sh /mnt/file4 
0	/mnt/file4
[root@server ~]# du -sh /mnt/file5
0	/mnt/file5
[root@server ~]# find /mnt -size 20k    # 查找/mnt下大小为20k的文件
/mnt/file2
[root@server ~]# find /mnt -size -20k   # 查找/mnt下大小小于20k的文件
/mnt
/mnt/file1
/mnt/file4
/mnt/file5
[root@server ~]# find /mnt -size +20k   # # 查找/mnt下大小大于20k的文件
/mnt/file3
4.按照时间(分钟)查找被篡改的文件
find  /mnt  -cmin   +10     # 查找/mnt下文件更新距离现在超过10分钟的文件
find  /mnt  -cmin   -10     # 查找/mnt下文件更新距离现在10分钟以内的文件
find  /mnt  -cmin    10     # 查找/mnt下文件更新距离现在10分钟的文件 
5.按照时间(天)查找被篡改的文件
find  /mnt  -ctime    10   # 查找/mnt下文件更新距离现在为10天的文件
find  /mnt  -ctime   -10   #  查找/mnt下文件更新距离现在在10天以内的文件
find  /mnt  -ctime   +10   #  查找/mnt下文件更新距离现在超过10天的文件 

[root@server ~]# find /mnt -cmin 10
[root@server ~]# find /mnt -cmin -10
/mnt/file1
/mnt/file2
/mnt/file3
[root@server ~]# find /mnt -cmin +10
/mnt
/mnt/file4
/mnt/file5 
6.按照文件类型查找文件
find  /mnt   -type  f # 查找/mnt下的文件
f   # 表示普通文件
d  # 表示目录
b  # 块设备文件
c  # 字符设备文件
l   # 符号连接文件
p  # 管道文件

[root@server ~]# find /mnt -type f
/mnt/file1
/mnt/file2
/mnt/file3
/mnt/file4
/mnt/file5
[root@server ~]# find /mnt -type d/mnt 
7.按照文件权限查找文件
find /mnt -perm 404  # 查找/mnt下权限为404的文件
find /mnt -perm -404  # 查找/mnt下权限包含404的文件
find /mnt -perm /404  # 查找/mnt下权限包含404任意一个的文件 

[root@server ~]# cd /mnt
[root@server mnt]# chmod 404 file1   # 改变文件的权限,方便测试
[root@server mnt]# chmod 444 file2
[root@server mnt]# chmod 644 file3
[root@server mnt]# chmod 640 file4
[root@server mnt]# chmod 000 file5
[root@server mnt]# ll	

在这里插入图片描述

[root@server mnt]# find /mnt -perm -644
/mnt
/mnt/file3
[root@server mnt]# find /mnt -perm 404
/mnt/file1
[root@server mnt]# find /mnt -perm /444
/mnt
/mnt/file1
/mnt/file2
/mnt/file3
/mnt/file4 
8.用find命令来执行一些动作
-exec   # 表示开始执行动作
{ }    # 表示find命令所查找出来的所有东西

[root@server mnt]# ll    # 加权限之前	

在这里插入图片描述

[root@server mnt]# find /mnt -perm -004 -exec chmod g+w {} \;  # 给/mnt下权限包含004的文件g位加w的权限,\ 表示转义  “;”表示命令的结束
[root@server mnt]# ll /mnt

在这里插入图片描述

[root@server mnt]# rm -fr *
[root@server mnt]# find /  -group mail -exec cp -rp {}  /mnt \;  # 把系统中属于mail组的文件备份到/mnt下
[root@server mnt]# ll /mnt 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值