一、find命令
实时查找,一般首先使用locate命令去进行查找,在使用find命令去查找,因为find命令是实时查找,耗费资源。
1、-name参数
find 位置 -name filename ### 在address位置下查找filename文件
[root@bogon opt]# find / -name gushi.txt ### 在根目录下查找文件
/opt/gushi.txt
模糊搜索:使用通配符进行搜索
常见的压缩包文件后缀:.tat .tgz .zip .tar.gz
使用find查找所有压缩的文件
find / -type f -name "*.tar" -o -name "*.tgz" -o -name "*.zip" -o -name "*.tar.gz"
通过find命令进行通配符的查找时,尽量加上引号(单引号、双引号均可)
2、-type
-type f 找文本文件
-type F 找目录
-type l 找链接
find 位置 -type f -name filename
根据这个命令就可以区分出文件和文件夹了
3、-exec参数
和xargs命令一样,都是将前面命令得到的内容传递给-exec在进行处理,-exec是find命令提供的参数,在执行删除命令时,比xargs更安全
在使用-exec时需要在命令的末尾加上’;’使用{}表示find命令查找到的数据
⚠️find命令在查找时,是查找到一个就把查找到的内容传递给{},进行二次加工,而不是在find查找完成后一次性将所有查找到的数据给{},在进行二次加工
[root@bogon opt]# touch {1..10}.txt
[root@bogon opt]# find . -name '*.txt' -exec rm -rf {} \;
4、-OK参数
和-exec参数一样,只不过是-OK多了一个确认的过程
5、根据时间查找
find . -name ‘filename’ -mtime(或者atime、ctime) +3(几天前)/-3(几天内)/3(前几天)
touch -d 修改后的时间 -m/-a/-c filename 去修改文件对应的时间
# 查找三个月前的数据
find /etc -mtime +90
# 查找最近十天之内的数据
find /etc -mtime -10
6、-o参数
-o是或者的意思
-o可以实现查找多个文件名
也可以实现多个参数,进行或者查找,满足其中一个条件即可
# 查找address目录下filename或者filename2或者filename3的文件
find address -name 'filename' -o 'filename2' -o 'filename3'
# 查找/boot目录下大小等于10M或者是文本文件的文件
find /boot -size 10M -o -type f
7、-l参数
-l参数:过滤出来的内容只显示文件名
[root@bogon opt]# grep -Rlni 'password' /var/log
/var/log/audit/audit.log.2
/var/log/audit/audit.log.1
/var/log/audit/audit.log
/var/log/messages-20231105
/var/log/anaconda/anaconda.log
/var/log/anaconda/syslog
/var/log/anaconda/journal.log
/var/log/messages-20231127
/var/log/boot.log-20231124
/var/log/messages-20231113
/var/log/boot.log-20231119
/var/log/boot.log-2023113
8、-maxdepth
find / -maxdepth number -type f 'filename'
从根目录开始搜索深度为number的目录层级
-maxdepth参数:以用户给定的位置为起点,给搜索设定最深搜索层级
9、-perm参数
使用-perm参数可以按照文件的权限去进行搜索
find / -perm 777
[root@bogon ~]# find /opt -perm 777
/opt/cmatrix-1.2a
10、-size
找出大小超过20MB的压缩包
find / -type f -size +20M -name "*.tar"
-20M ##小于20MB
20M ##等于20MB
11、-user
按照数据的所有者进行查询
find /boot -user root
二、locate命令
locate是进行索引查找,他的查找是存在延迟的,比方说我们刚刚创建的一个文件使用locate命令是不能立刻查找出来的,
locate的索引在/var/lib/mlocate/mlocat.db数据库中,Linux的数据库默认是在0点去更新,只有在更新数据库后才可以使用locate命令去查找出来
我们也可以使用undatedb去强制更新数据库更新后也可以查出
cat.db数据库中,Linux的数据库默认是在0点去更新,只有在更新数据库后才可以使用locate命令去查找出来
我们也可以使用undatedb去强制更新数据库更新后也可以查出
[外链图片转存中...(img-wv2craTA-1716978622433)]
#