find文件查找

find基础语法

find [路径] [选项] [表达式] [动作]

find选项

按文件类型查找

-type
f:可编辑的文件
d:目录
l:软链接文件
b:块设备文件 磁盘,U盘 /dev/sda
c:字符设备文件 终端
s:socket 安全套接字文件
p:管道文件

find [路径] [选项]
[root@wjh ~]# find /run -type s
# 查看找出文件的详细信息
[root@wjh ~]# find /run -type s|xargs ls -l

# 查询/etc/目录下所有目录,总共有多少个?
[root@wjh ~]# find /etc -type d|wc -l
596

按文件大小查找

-size
-:小于
+:大于
Num:精准但是又不精准的匹配



# 1.在/opt下创建1000个文件
# 2.使用find找出(/opt下小于1k的文件)并删除
[root@wjh ~]# find /opt/ -size -1k|xargs rm -fr


# 1.在/opt下创建1000个文件
# 2.使用find找出(/opt下小于1k的文件)把它们全部移动到/tmp下

mv 源文件 目标路径
mv -t 目标路径 源文件
#查找/opt/下小于1k的文件放到/tmp下
[root@wjh ~]# find /opt/ -size -1k |xargs mv -t /tmp

xargs:
-i:指定数据流的位置,将数据流放入{}中
#查找/opt/下小于1k的文件放到/tmp下
[root@wjh ~]# find /opt/ -size -1k |xargs -i mv {} /tmp

按文件名查找

-name:严格区分大小写
#查找名字为wjh的
[root@wjh ~]# find / -name 'wjh'
#查找名字为..wjh的
[root@wjh ~]# find / -name '*wjh'
#查找名字为wjh..的
[root@wjh ~]# find / -name 'wjh*'
#查找名字为..wjh..的
[root@wjh ~]# find / -name '*wjh*'

-iname:不区分大小写
[root@wjh ~]# find / -iname 'wjh'

按文件时间查找

-atime:文件访问时间差找
-mtime:文件内容创建,修改时间差找
-ctime:文件属性,修改时间差找

Num:查找第N天的文件(不包括今天)
+Num:查找第N天之前的所有文件(不包括今天)
-NUm:查找从今天开始算,7天内的文件

# 一个文件有以下三种时间
access time:atime
modify time:mtime
change time:ctime

# 查看三种时间
[root@wjh <sub>]# stat wjh.txt
Access: 2022-04-17 12:24:57.385996021 +0800
Modify: 2022-04-17 12:24:57.385996021 +0800
Change: 2022-04-17 12:24:57.385996021 +0800

for i in `seq -w 30`;do date -s 202204$i && touch file-$i;done
## 保留近七天的文件
[root@wjh ~]# find /opt ! -mtime -7|xargs rm -f
[root@wjh ~]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 24 00:00 file-24
-rw-r--r-- 1 root root 0 Apr 25 00:00 file-25
-rw-r--r-- 1 root root 0 Apr 26 00:00 file-26
-rw-r--r-- 1 root root 0 Apr 27 00:00 file-27
-rw-r--r-- 1 root root 0 Apr 28 00:00 file-28
-rw-r--r-- 1 root root 0 Apr 29 00:00 file-29
-rw-r--r-- 1 root root 0 Apr 30 00:00 file-30

按照文件用户和组查找

-user:查找文件的属主
-nouser:文件没有属主用户的

-group:查找文件的属组
-nogroup:文件没有属组的

多条件组合查找

#查找opt下近7天修改的文件
[root@wjh ~]# find /opt/ -mtime -7
/opt/
/opt/file-24
/opt/file-25
/opt/file-26
/opt/file-27
/opt/file-28
/opt/file-29
/opt/file-30
#查找opt下近7天修改的目录
[root@wjh ~]# find /opt/ -type d -mtime -7
/opt/
/opt/dir-24
/opt/dir-25
/opt/dir-26
/opt/dir-27
/opt/dir-28
/opt/dir-29
/opt/dir-30
#查找opt下名字是dir..的还有名字是..0的,并且7天内修改且小于1k的文件
[root@wjh]# find /opt/ -name 'dir*' -name '*0' -type f -mtime -7 -size +1K

按权限查找

-perm

## 权限精确查找
[root@wjh ~]# find ./ -perm 222
[root@wjh ~]# find ./ -perm 644
./.bash_logout
./.bash_profile
./.cshrc
./.tcshrc
./.bashrc.swp
./.bashrc
./wjh.txt

## -权限
# 每个权限位上,都要包含该数字权限的所有权限
[root@wjh ~]# find ./ -perm -622 -ls
622
rw--w--w-

# /权限
# 总共三个权限位,只要有一个权限位的权限被包含,就可以找到
[root@wjh ~]# find ./ -perm /644 -ls
属主权限位,有一个r或者有一个w就满足条件
属组权限位,有一个r就满足条件
其他用户权限位,有一个r就满足条件

按深度查找

-maxdepth
针对目录层级查找
# 查找/etc/目录下的所有1级和2级目录
[root@wjh ~]# find /etc -type d -maxdepth 2

find动作

-print:打印查找到的内容到终端上(find命令默认就是打印结果 -print

-ls:查看文件的详细信息 |xargs ls -l 或者 ls -l $(find xxx) 或者 ls -l `find xxx`

-delete:删除查找到的文件(bug跟ls,ls看不见的,也删除不掉)并且只能删除空目录

其他删除方法: |xargs rm -fr 或者 rm -fr $(find xxx) 或者 rm -fr `find xxx`
#查找删除名字为'etc'd的文件或者目录
[root@wjh ~]# find / -name 'etc' -delete
#无法删除'/run/initramfs/state/etc':目录不为空
find: cannot delete ‘/run/initramfs/state/etc’: Directory not empty
#无法删除“/ etc ”:目录未为空
find: cannot delete ‘/etc’: Directory not empty
#无法删除“/ var / tmp / etc ”:目录不为空
find: cannot delete ‘/var/tmp/etc’: Directory not empty

-ok:找到文件后,执行后面的bash命令,询问是否要操作
语法:-ok 系统命令 {} \;
find / -type f -name 'file*' -ok mv {} /tmp \;

-exec:找到文件后,执行后面的bash命令
语法:-exec 系统命令 {} \;
find / -type f -name 'file*' -exec mv {} /tmp \;

find多条件

-a:和,并且(默认)
-o:或者
!:取反

佳豪哥哥教你学Linux的第十九天 _bash