使用linux find命令进行 查找文件

作者

linux find 命令,是linux环境下进行文件查找的命令。文件查找是服务器管理基本操作。find命令本身也很强大,可以按照名字、所有者、时间、权限等方式进行查找。这里总结find一些常用的用法。

linux-find文件查找
linux-find文件查找

当前目录下查找名字是 passwd 文件

find . -name "passwd"

/etc 目录下查找 passwd 文件

find /etc -name "passwd"

find 文件查找不区分大小写

find /etc -iname "PassWD"

find 使用通配符进行查找

find /etc -name "*.conf"
find /etc -name "*.con*"
# 如果名字不用双引号,后通配符必须转义
find /etc -name *.con\*

find 查找限制查找的深度

find /etc -maxdepth 1  -name "*.conf" 

find 查找指定用户的文件

find /etc -user root  -name "*.conf"

find 指定查找的文件类型

# 使用 type -c
# b 块文件
# c 字符文件
# d 目录
# p pipe 文件
# f 普通文件
# l 链接文件

find /etc -type f -name "*.conf"

find 按照时间查找

# 通过数字指定范围
# -n 多少(天,分钟)以内的文件
# +n 超过多少(天,分钟)的文件
# n 刚好多少(天,分钟)的文件

find /etc -mtime -2  # 表示   现在到 2*24 小时内修改的文件 
find /etc -mtime 2   # 表示  48小时(2天*24) ~ 72(3天*24)小时内修改文件, 返回的结果的文件前后被修改的时间不超过48小时
find /etc -mtime +5  # 表示  (5+1)天前被修改的文件


# 按照分钟计算
find /etc -mmin -2
find /etc -mmin 2
find /etc -mmin +2

find 按照权限查找

# perm ,权限的匹配有三种目录
# 1. 精准匹配 
# 2. 包含匹配  -
# 3. 任意一位匹配  /

touch file-400.txt
touch file-440.txt
touch file-444.txt
touch file-600.txt
touch file-640.txt
touch file-644.txt

chmod 400 file-400.txt
chmod 440 file-440.txt
chmod 444 file-444.txt
chmod 600 file-600.txt
chmod 640 file-640.txt
chmod 644 file-644.txt

# 同时满足 用户、组、所有者都用 读的权限
find . -perm -444 -type f
./file-444.txt
./file-644.txt

# 只要满足 400,  所有者有读权限接口
find . -perm -400 -type f
./file-400.txt
./file-440.txt
./file-444.txt
./file-600.txt
./file-640.txt
./file-644.txt


# 没有满足的条件
find . -perm -420 -type f


# 满足一个即可
find . -perm /420 -type f
./file-400.txt
./file-440.txt
./file-444.txt
./file-600.txt
./file-640.txt
./file-644.txt


# 具体替换成 2进制就可以比较

find 跳过或者排除指定的目录

# 用来查找备份文件
# ! 用啦排除路径, !后紧跟的是要排除的路径, 
#       需要注意的地方是被排除的路径,不是递归操作,意思是他的子目录,还是在查找的范围内
#       通过公配符号 * 可以实现递归排除
#       这里忽略了两个目录
find / ! -path '/proc/*' ! -path '/var/*'   -name "*.backup"

# 递归忽略 /proc 的下的文件,输出的结果会打印 /proc
find / -path '/proc' -prune -o  -name "*.backup"

find 按照大小查找

# b 512-bype , c bytes, w - two-byte words, 
# k 1024bytes, M 兆, G 

find /  -path '/proc/*' -prune -size +100M -type f

find 使用正则表达式匹配名字

find . -regex ".*file-44.*"
./file-440.txt
./file-444.txt

# 不区分大小写
find . -iregex ".*F.*"

find 查找空文件

find . -empty

find 查找可执行的文件

find . -executable 

find 比较文件时间进行查找

# a.txt 更晚产生的文件
find . -newer a.txt

find 对查询的获得的文件,进一步处理

# 删除
find . -name "*.logs" -delete

# 执行命令
find . -name "*.logs" -exec rm -fr {} \;

回复

您的电子邮箱地址不会被公开。