find命令详解

1,find的作用
find是linux系统中用来进行文件查找的工具

2,为什么用find来查找文件
因为find命令可以根据不同的条件来进行文件查找
利如:文件名称
文件时间
文件大小
文件属主属组
文件权限
等等。。。。。

3,find命令语法

在这里插入图片描述

4,find查找示例

4.1 基于名称查找

创建用于测试的文件
[root@nginx1 ~]# touch /find/{aaa.txt,AAA.txt}
[root@nginx1 ~]# ls /find/
aaa.txt AAA.txt

查找名称为aaa.txt的文件 (-name 精确匹配)
[root@nginx1 ~]# find / -name aaa.txt
/find/aaa.txt

忽略大小写查找(-iname 忽略大小写)
[root@nginx1 ~]# find / -iname aaa.txt
/find/aaa.txt
/find/AAA.txt

4.2 基于文件大小查找
查找大于5M的文件
[root@nginx1 ~]# find /etc/ -type f -size +5M
/etc/udev/hwdb.bin

[root@nginx1 ~]# find /etc/ -type f -size +5M |xargs ls -lh
-r–r--r–. 1 root root 8.5M 6月 14 19:28 /etc/udev/hwdb.bin

查找大小等于5M的文件
[root@nginx1 ~]# find /usr/lib -type f -size 5M
/usr/lib/firmware/netronome/flower/nic_AMDA0096.nffw
/usr/lib/firmware/netronome/flower/nic_AMDA0097.nffw
/usr/lib/firmware/netronome/flower/nic_AMDA0099.nffw
/usr/lib/firmware/netronome/nic-sriov/nic_AMDA0058-0011_2x40.nffw
/usr/lib/firmware/netronome/nic-sriov/nic_AMDA0058-0012_2x40.nffw

查找小于5M的文件

[root@nginx1 ~]# find /usr/local/ -size -5M
/usr/local/
/usr/local/bin
/usr/local/etc
/usr/local/games
/usr/local/include
/usr/local/lib
/usr/local/lib64
/usr/local/libexec
/usr/local/sbin

[root@nginx1 ~]# find /etc/ -size -5M |wc -l
2350

4.3 基于文件类型查找
-type f 文件
[root@nginx1 ~]# find /etc/init.d/ -type f
/etc/init.d/README
/etc/init.d/functions
/etc/init.d/netconsole
/etc/init.d/network

-type d 目录
[root@nginx1 ~]# find /etc/sysconfig/ -type d
/etc/sysconfig/
/etc/sysconfig/cbq
/etc/sysconfig/console
/etc/sysconfig/modules
/etc/sysconfig/network-scripts

-type l 链接
[root@nginx1 ~]# find /etc/rc0.d/ -type l
[root@nginx1 ~]# find /dev/ -type l | xargs ls -lh
lrwxrwxrwx 1 root root 6 6月 20 16:14 /dev/block/11:0 -> …/sr0
lrwxrwxrwx 1 root root 7 6月 20 16:14 /dev/block/253:0 -> …/dm-0
lrwxrwxrwx 1 root root 7 6月 20 16:14 /dev/block/253:1 -> …/dm-1
lrwxrwxrwx 1 root root 6 6月 20 16:14 /dev/block/8:0 -> …/sda

-type b 块设备
[root@nginx1 ~]# find /dev/ -type b
[root@nginx1 ~]# find /dev/ -type b | xargs ls -lh
brw-rw---- 1 root disk 253, 0 6月 20 16:14 /dev/dm-0
brw-rw---- 1 root disk 253, 1 6月 20 16:14 /dev/dm-1
brw-rw---- 1 root disk 8, 0 6月 20 16:14 /dev/sda
brw-rw---- 1 root disk 8, 1 6月 20 16:14 /dev/sda1
brw-rw---- 1 root disk 8, 2 6月 20 16:14 /dev/sda2

-type c 字符设备
[root@nginx1 ~]# find /dev/ -type c | xargs ls -lh
crw------- 1 root root 10, 235 6月 20 16:14 /dev/autofs
crw------- 1 root root 248, 0 6月 20 16:14 /dev/bsg/1:0:0:0
crw------- 1 root root 248, 1 6月 20 16:14 /dev/bsg/2:0:0:0
crw------- 1 root root 10, 234 6月 20 16:14 /dev/btrfs-control
crw-rw-r-- 1 root root 189, 0 6月 20 16:14 /dev/bus/usb/001/001

-type s 套接字

[root@nginx1 ~]# find / -type s |xargs ls -lh
srw-rw-rw- 1 root root 0 6月 20 16:14 /dev/log
srw-rw-rw- 1 root root 0 6月 20 16:14 /run/dbus/system_bus_socket
srw------- 1 root root 0 6月 20 16:14 /run/lvm/lvmetad.socket
srw------- 1 root root 0 6月 20 16:14 /run/lvm/lvmpolld.socket
srwx------ 1 root root 0 6月 20 16:14 /run/systemd/cgroups-agent
srw-rw-rw- 1 root root 0 6月 20 16:14 /run/systemd/journal/socket

-type p 管道文件
[root@nginx1 ~]# find / -type p |xargs ls -lh
prw------- 1 root root 0 6月 20 16:14 /run/dmeventd-client
prw------- 1 root root 0 6月 20 16:14 /run/dmeventd-server
prw------- 1 root root 0 6月 20 16:14 /run/systemd/inhibit/1.ref
prw------- 1 root root 0 6月 20 16:14 /run/systemd/initctl/fifo
prw------- 1 root root 0 6月 20 16:15 /run/systemd/sessions/1.ref

4.4 基于时间的查找

-mtime(文件创建时间),-ctime(文件最后修改时间)
在这里插入图片描述
创建测试文件
[root@nginx1 ~]# for i in {11…20};do date -s 2021/06/ i ; t o u c h / f i n d / f i l e − i;touch /find/file- i;touch/find/filei;done

[root@nginx1 ~]# ls -al /find/
总用量 0
drwxr-xr-x 2 root root 201 6月 20 00:00 .
dr-xr-xr-x. 18 root root 256 6月 20 2021 …
-rw-r–r-- 1 root root 0 6月 20 2021 aaa.txt
-rw-r–r-- 1 root root 0 6月 20 2021 AAA.txt
-rw-r–r-- 1 root root 0 6月 11 00:00 file-11
-rw-r–r-- 1 root root 0 6月 12 00:00 file-12
-rw-r–r-- 1 root root 0 6月 13 00:00 file-13
-rw-r–r-- 1 root root 0 6月 14 00:00 file-14
-rw-r–r-- 1 root root 0 6月 15 00:00 file-15
-rw-r–r-- 1 root root 0 6月 16 00:00 file-16
-rw-r–r-- 1 root root 0 6月 17 00:00 file-17
-rw-r–r-- 1 root root 0 6月 18 00:00 file-18
-rw-r–r-- 1 root root 0 6月 19 00:00 file-19
-rw-r–r-- 1 root root 0 6月 20 00:00 file-20

查找7天之前的文件 (+7)
[root@nginx1 ~]# find /find -type f -mtime +7 |xargs ls -al
-rw-r–r-- 1 root root 0 6月 11 00:00 /find/file-11
-rw-r–r-- 1 root root 0 6月 12 00:00 /find/file-12

查找第7天的文件(7)
[root@nginx1 ~]# find /find -type f -mtime 7 |xargs ls -al
-rw-r–r-- 1 root root 0 6月 13 00:00 /find/file-13

查找最近七天的文件(-7)
[root@nginx1 ~]# find /find -type f -mtime -7 |xargs ls -al
-rw-r–r-- 1 root root 0 6月 20 2021 /find/aaa.txt
-rw-r–r-- 1 root root 0 6月 20 2021 /find/AAA.txt
-rw-r–r-- 1 root root 0 6月 14 00:00 /find/file-14
-rw-r–r-- 1 root root 0 6月 15 00:00 /find/file-15
-rw-r–r-- 1 root root 0 6月 16 00:00 /find/file-16
-rw-r–r-- 1 root root 0 6月 17 00:00 /find/file-17
-rw-r–r-- 1 root root 0 6月 18 00:00 /find/file-18
-rw-r–r-- 1 root root 0 6月 19 00:00 /find/file-19
-rw-r–r-- 1 root root 0 6月 20 00:00 /find/file-20

4.5,基于用户的查找

查找/home/下属主和数组都是whp的文件 (-user 指定用户,-group 指定属组,-a两个条件必须同时成立)
-a 两个条件必须同时满足
-o 只要有个一条件满足就行了
!取反

[root@nginx1 ~]# find /home/ -type f -user whp -a -group whp |xargs ls -l
-rw-r–r-- 1 whp whp 18 4月 1 2020 /home/whp/.bash_logout
-rw-r–r-- 1 whp whp 193 4月 1 2020 /home/whp/.bash_profile
-rw-r–r-- 1 whp whp 231 4月 1 2020 /home/whp/.bashrc

[root@nginx1 ~]# chown root /home/whp/.bash_logout
[root@nginx1 ~]# find /home/ -type f -user whp -o -group whp |xargs ls -l
-rw-r–r-- 1 root whp 18 4月 1 2020 /home/whp/.bash_logout
-rw-r–r-- 1 whp whp 193 4月 1 2020 /home/whp/.bash_profile
-rw-r–r-- 1 whp whp 231 4月 1 2020 /home/whp/.bashrc

找出系统中没有属主和属组的文件并且删掉(-nouser 没有属主,-nogroup 没有属组

[root@nginx1 ~]# userdel whp
[root@nginx1 ~]# ls -al /home/whp/
-rw-r–r-- 1 root 1000 18 4月 1 2020 .bash_logout
-rw-r–r-- 1 1000 1000 193 4月 1 2020 .bash_profile
-rw-r–r-- 1 1000 1000 231 4月 1 2020 .bashrc

[root@nginx1 ~]# find /home/ -type f -nouser -a -nogroup |xargs rm -f
[root@nginx1 ~]# ls -al /home/whp/
-rw-r–r-- 1 root 1000 18 4月 1 2020 .bash_logout

找出除某个文件外的其他文件
[root@nginx1 ~]# find /find/ ! -name file-11 (除了file-11,其他文件都找出来)
/find/
/find/aaa.txt
/find/AAA.txt
/find/file-10
/find/file-12
/find/file-13
/find/file-14
/find/file-15
/find/file-16
/find/file-17
/find/file-18
/find/file-20
/find/file-19

4.6,基于权限的查找

精确查找(查找权限为444的文件)
[root@nginx1 find]# find /find/ -type f -perm 444 |xargs ls -l
-r–r--r-- 1 root root 0 6月 18 00:00 /find/file-18

模糊查找 (-444 表示大于或者等于444权限的都能匹配到)
[root@nginx1 find]# find /find/ -type f -perm -444 |xargs ls -l
-rw-r–r-- 1 root root 0 6月 20 16:18 /find/aaa.txt
-rw-r–r-- 1 root root 0 6月 20 16:18 /find/AAA.txt
-rw-r–r-- 1 root root 0 6月 10 00:00 /find/file-10
-rw-r–r-- 1 root root 4 6月 20 00:11 /find/file-11
-rw-r–r-- 1 root root 0 6月 12 00:00 /find/file-12
-rw-r–r-- 1 root root 0 6月 13 00:00 /find/file-13
-rw-r–r-- 1 root root 0 6月 14 00:00 /find/file-14
-rw-r–r-- 1 root root 0 6月 15 00:00 /find/file-15
-rw-r–r-- 1 root root 0 6月 16 00:00 /find/file-16
-rw-r–r-- 1 root root 0 6月 17 00:00 /find/file-17
-r–r--r-- 1 root root 0 6月 18 00:00 /find/file-18

(/133,属主没有执行(1)权限并且属组没有写(2)权限(1)执行权限同时其他组没有写(2)权限(1)执行权限)的不显示。
[root@nginx1 find]# find /find/ -type f -perm /133 |xargs ls -l
-rwx-wx-wx 1 root root 4 6月 20 00:09 /find/file-19
-rwx-w–w- 1 root root 0 6月 20 00:00 /find/file-20

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值