2020-3-19 linux 基础 find

1.find命令

find命令可以根据不同的条件来定位到想要查找的文件,例如 名称、文件大小、修改时间、属主、属组等等。

2.find 查找命令语法

命令 路径 选项 表达式 动作
find / /etc options expression actions
查找 地区 妹子 18-25岁

3.find 查找文件 find

选项 ==-o== 或者

3.1 find 名称查找文件 -name

find 路径 -name “xxx” 区分大小写

[root@wangjc ~]# touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}
[root@wangjc ~]# find /etc -name "*eth1"
/etc/sysconfig/network-scripts/ifcfg-eth1

find 路径 -iname “*name” 不区分 大小写

[root@wangjc ~]# find /etc -iname "*eth1"
/etc/sysconfig/network-scripts/ifcfg-eth1
/etc/sysconfig/network-scripts/IFCFG-ETH1

3.2 find 文件大小查找 -size

find 路径 -size +5M (大于) -5M(小于)

[root@wangjc ~]# find / -size +100M |xargs ls -lh
find: ‘/proc/8053/task/8053/fd/6’: No such file or directory
find: ‘/proc/8053/task/8053/fdinfo/6’: No such file or directory
find: ‘/proc/8053/fd/5’: No such file or directory
find: ‘/proc/8053/fdinfo/5’: No such file or directory
-r--------. 1 root root 128T Mar 19 10:04 /proc/kcore
-rw-------. 1 root root 128M Mar 19 09:58 /sys/devices/pci0000:00/0000:00:0f.0/resource1
-rw-------. 1 root root 128M Mar 19 09:58 /sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
-rw-r--r--. 1 root root 102M Feb 28 11:49 /usr/lib/locale/locale-archive

3.3 find 文件类型查找 -type

类型符号 类型
-type f 文件类型
-type d 目录类型
-type l 链接类型
-type b 块设备(磁盘)
-type c 字符设备
-type s 套文字 (进程之间通信文件)
[root@wangjc ~]# find  / -type d |wc -l    #查找/  下所有目录文件   统计
19082
[root@wangjc ~]# find  / -type f |wc -l     #查找 / 下所有文件类型文件  统计
122240
[root@wangjc ~]# find / -type l |wc -l      #查找 / 下所有链接文件类型文件  统计
11391
[root@wangjc ~]# find / -type c |wc -l      #查找 / 下所有字符设备类型文件  统计
145
[root@wangjc ~]# find / -type b |wc -l       #查找 / 下所有块设备类型文件  统计
7

3.4 find 时间查找 -mtime

命令符
-mtime +7 7天以前的文件 不包括当天
-mtime 7 第7天的文件 不包括当天
-mtime -7 查找最近7天的 包括当天

保留最近30天内的文件

find /var/log -type f -name "*.tar.gz" -mtime +30  -delete
QQ图片20200319104955.jpg

3.5 find 用户查找 -user 属组 -group

命令符 作用
-user xxx 属主 是xxx的文件
-group xxx 属组 是xxx的文件
-nouser 没有属主的
-nogroup 没有属组的

3.6 find 和| xargs grep “xxx” 结合使用查找文件内容

find命令 不能查询文件内容

只记得文件内容 和大概路径

find /xxx -type f |xargs grep "内容"

4.find 动作 查找文件后的操作

命令符 作用
-print 默认
-ls
-delete 删除文件
-ok (-ok {} \;) 自定义命令
-exec -exec cp {} /tmp ; 不提示 自定义命令
[root@wangjc ~]# find ./ -type f -name "file*"
./file.txt
./file2.txt
./file3.txt
[root@wangjc ~]# find ./ -type f -name "file*"  -exec rm -f {} \;
[root@wangjc ~]# find ./ -type f -name "file*"  
[root@wangjc ~]# 

5.find 逻辑运算符

逻辑符
-a and
-o or 或者
! 或者 -not 非(不是)

你可能感兴趣的:(2020-3-19 linux 基础 find)