2024年5月10日发(作者:)

linux的find的用法

find 是 Linux 系统中一个非常强大的命令,用于在文件系统中搜索文件和目录。以下是

find 命令的一些基本用法和常见示例:

基本用法:

bash

find [路径] [表达式]

按名称搜索:

bash

find /path/to/search -name "filename"

按类型搜索:

搜索所有目录: -type d

搜索所有文件: -type f

搜索符号链接: -type l

bash

find /path/to/search -type d # 搜索所有目录

find /path/to/search -type f # 搜索所有文件

find /path/to/search -type l # 搜索所有符号链接

按大小搜索:

搜索大于某个大小的文件: -size +size

搜索小于某个大小的文件: -size -size

搜索精确大小的文件: -size n[cwbkMG]其中 n 是数字,c 是字节,w 是字,b 是块,k 是

KB,M 是 MB,G 是 GB。

bash

find /path/to/search -size +100M # 搜索大于100MB的文件

find /path/to/search -size -100k # 搜索小于100KB的文件

按时间搜索:

搜索最近 n 天修改的文件: -mtime n

搜索在 n 天前至现在修改的文件: -mtime +n

搜索在 n 天前以前至更早修改的文件: -mtime -n

搜索在最近 n 天内修改过的文件: -mtime n (注意 n 前面没有正负号)

bash

find /path/to/search -mtime 3 # 搜索最近3天内修改过的文件

按权限搜索:

搜索权限为 r 的文件: -readable

搜索权限为 w 的文件: -writable

搜索权限为 x 的文件: -executable

bash

find /path/to/search -readable # 搜索可读的文件

组合条件搜索:

使用逻辑运算符 -and, -or, 和 -not 来组合多个条件。例如:

bash

find /path/to/search -name "*.txt" -and -size +1M # 搜索所有大于1MB的txt文件

忽略某些路径:

如果你想在搜索中忽略某些路径,可以使用 ! -path。例如,要查找 /home/user 但忽略

/home/user/tmp:

bash

find /home/user ! -path "/home/user/tmp*"

这只是 find 命令的冰山一角。它有很多选项和参数,可以通过 man find 查看详细的手册

来深入了解它的用法。