linux命令学习之find命令的使用查找文件 超详细

find 命令是 Linux 系统上用于文件搜索的强大工具。
它支持按名称、类型、时间、大小等条件进行过滤,并且可以与其他命令组合来执行操作。
基本语法 search [路径] [选项] [操作] 路径:指定搜索的起始目录(默认为当前目录)。
选项:指定搜索条件(例如文件名、类型、时间等)。
Action:对匹配结果采取的操作(例如删除、修改权限等)。
常用功能详细说明 按精确匹配名称搜索:搜索/路径名“*.txt”(细节敏感)。
忽略大小写:find/path-iname "*.txt" 多个条件匹配:find.-name "*.txt" -o-name "*.pem" (-o 表示“或”)。
按路径匹配 使用-path 来匹配路径中的指定字符串,例如find/usr-path "*local*"。
-regex 用于将复杂的路径模式与正则表达式进行匹配;示例:find.-正则表达式“.*.(txt|pem)$”。
按文件类型指定搜索类型(例如普通文件类型f、目录类型);示例:find.-maxdepth3 -typef(将搜索深度限制为 3 )。
按时间(atime)过滤:find.-typef-atime-7 (7 天内访问的文件)。
修改时间(mtime):find.-typef-mtime+7 (超过7 天没有修改过的文件)。
权限变更时间(ctime):find.-typef-ctime-7 (7 天内权限变更的文件)。
按文件大小单位过滤:k(千字节)、M(兆字节)、c(字节)。
例如:find.-size+1 0k(大于1 0KB的文件)。
高级操作删除文件:find.-name"*.tmp"-delete。
修改权限/所有者:find.-userroot-execchowntom{}; (将匹配文件的所有者更改为 tom)。
反之亦然删除-~-name"*.log"-okrm{}; (删除前需要确认。
) 排除目录 使用-prune 跳过指定目录,例如find.-path"./abc"-prune-o-name"*.txt"-print(跳过abc 目录查找.txt 文件)。
注意:权限问题:如果搜索系统目录(如/),则需要使用sudo。
性能优化:限制搜索深度(-max深度)或范围可以提高速度。
安全删除:建议通过-execls -l{}预览匹配文件。
在执行删除之前。
示例场景查找并删除最近 3 0 天的日志文件:find/var/log-name"*.log"-mtime+3 0-execrm{};统计当前目录下所有.py文件的行数:find.-name "*.py"-execwc -l{};通过灵活的组合选择; find命令可以高效地完成复杂的文件管理任务。

linux命令之find命令的12个常用参数详解含具体用法和注意事项

1 、使用name选项:filename选项是find命令最常用的选项,可以单独使用,也可以与其他选项结合使用。
您可以使用文件名模式来匹配文件,请记住在引号中包含文件名模式。
无论当前路径是什么,如果您想在根 $HOME 目录中查找文件名与 *.log 匹配的文件,请使用 ~ 作为“路径名”参数,波浪号 ~ 代表您的 $HOME 目录。
复制代码如下:find~-name "*.log"-print 如果要查找当前目录及子目录中所有'*.log'文件,可以使用: 复制代码如下:find.-name "*.log"-print 要在当前目录和想要的子目录中查找文件名以大写字母开头的文件,可以使用"scopy"-name。
如果要在 /etc 目录下查找名称以 host 开头的文件,可以使用: 复制代码如下: find/etc-name "host*" -print 如果要在 $HOME 目录下查找文件,可以使用: 复制代码如下: find~ -name "*" -print 或 find.-print 如果希望系统在所有加载的文件上运行,则在根目录下查找。
复制代码如下: find/-name "*" -print 如果要查找当前目录下名称以小写字母开头,以 4 到 9 结尾的文件 add .log: 命令: 复制代码如下: find.-name "[a-z]*[4 -9 ].log" -print 输出: 复制代码如下: [root@local#llhtotal 3 1 6 -rw-r--r--1 rootroot3 02 1 08 1 1 -1 3 06 :03 log2 01 2 .log-rw-r--r--1 rootroot6 1 1 1 -1 3 06 :03 log2 01 3 .log-rw-r--r--1 rootroot01 1 -3 2 01 4 .log-rw-r--r--1 rootroot01 1 -1 3 06 :06 log2 01 5 .logdrwxr-xr-x6 rootroot4 09 6 1 0-2 7 01 :5 8 scfdrwxrwxr-x2 rootroot4 09 6 1 1 -1 3 06 :wx2 rxroot 09 6 1 1 -1 3 05 :5 0test4 [root@localhosttest]#find.-name"[a-z]*[4 -9 ].log"-print./log2 01 4 .log./log2 01 5 .log./test4 /log2 01 4 .log[root@localhosttest]#2 .使用perm选项:按文件权限模式使用-perm选项,按文件权限模式搜索文件。
权限最好使用八进制表示法。
如果你想在当前目录下查找一个具有7 5 5 位文件权限的文件,即该文件的所有者可以读、写、执行,其他用户可以读、执行该文件,可以使用:复制代码如下:[root@localhosttest]#find.-perm7 5 5 -print。
./scf./scf/lib./scf/service./scf/service/deploy./scf/service/deploy/product./scf/service/deploy/info./scf /doc./scf/bin[root@localhosttest]# 还有另一种说法:在八进制数字前面添加一个连字符 - 以表明它们全部匹配。
例如,-007 等于 7 7 7 ,-005 等于 5 5 5 命令:复制代码。
代码如下: fin d.-perm-005 输出: 复制代码如下: [root@localhosttest]#ll Total 3 1 6 -rw-r--r--1 rootroot3 02 1 08 1 1 -1 3 06 :03 log2 01 2 .log-rw-r--r --1 rootroot6 1 1 1 -1 3 06 :03 log2 01 3 .log-rw-r--r--1 rootroot01 1 -1 3 06 :03 log2 01 4 .log-rw-r--r--1 rootroot01 1 -1 3 06 :0 6 log2 01 5 .logdrwxr-xr-x6 rootroot4 09 6 1 0-2 7 01 :5 8 scfdrwxrwxr-x2 rootroot4 09 6 1 1 -1 3 06 :08 test3 drwxrwxr-x2 rootroot 4 09 6 1 1 -1 3 05 :5 0test4 [root@localhosttest]#find.-perm-005 ../test4 ./scf./scf/lib./scf/service./scf/service/deploy./scf/service/deploy/product./scf/service/deploy/info./scf/bin./scf/servis 忽略目录: 如果要在搜索时忽略目录因为你知道你要查找的文件不存在于该目录中,也就是说,你可以使用 -prune 选项来指示要忽略哪些目录。
使用 -prune 选项时要小心,因为如果还使用 -depth 选项,则 -prune 选项将被 find 命令忽略。
如果想在 test 目录下查找文件,但不想在 test/test3 目录下查找,可以使用: 命令:复制代码如下: findtest-path "test/test3 "-prune-o-print 输出:复制代码如下:[root@localhostsoft]#findtest-path "test/test3 "-prune-o-printtesttest/log2 01 4 .logtest/log2 01 5 .logtest/test4 test/test4 /log2 01 4 .logtest /test4 /log2 01 3 .logtest/test4 /log2 01 2 .logtest/scftest/scf/libtest/scf/servicetest/scf/service/deploytest/scf/service/deploy/producttest/scf/service/deploy/infotest/scf/doctest/scf/bintest/log2 01 3 .logtest/log2 01 2 .loghost[01 2 .log2 01 4 ] 如何避免使用find查找文件时的某些文件目录: 示例1 :在test目录中查找不在test4 子目录中命令中所有文件: 复制代码如下: findtest-path "test/test4 "-prune-o-print 输出: 复制代码如下: [root@localhostsoft]#findtesttesttest/log2 01 4 .logtest/log2 01 5 .logtest/test4 test/test4 /log2 01 4 .logtest/test4 /log2 01 3 .logtest/test4 /log2 01 2 .logtest/scftest/sc f/libtest/scf/servicetest/scf/service/deploytest/scf/service/deploy/producttest/scf/service/deploy/infotest/scf/doctest/scf/bintest/log2 01 3 .logtest/log2 01 2 .logtest/test3 [root@localhostsoft]#findtest-path"test/test4 "-prune-o inttesttest/log2 01 4 .logtest/log2 01 5 .logtest/scftest/scf/libtest/scf/servicetest/scf/service/deploytest/scf/service/deploy/producttest/scf/service/deploy/infotest/scf/doctest/scf/bintest/log2 01 3 .logtest/log2 01 2 .logtest/log2 01 2 .logtest解释。
如下所示: 路径列表之后的 search[-path..][表达式] 是表达式 -path "test"-prune-o-print 是 -path "test"-a-prun 速记表达式 e-o-print 按顺序求值。
-a 和 -o 是短路计算,类似于 shell && 和 ||。
如果“test”-path为true,则评估-prune,-prune返回true,逻辑表达式为true;否则 -prun 不被求值,并且逻辑表达式为假。
如果 -path"test"-a-prune 为 false,则对 -print 求值,并且 -print 返回 true,或者逻辑表达式为 true; else -print 不被求值,或者逻辑表达式为 true。
这种表达式组合的特殊情况可以用伪代码编写为: if-path "test" then-pruneelse-print 示例 2 :避免多个文件夹: 命令:复制代码如下: findtest\(-pathtest/test4 -o-pathtest/test3 \)-prune-o-print 输出:复制代码如下: [root@localhostsoft]#findtest\(-pathtest/test4 -o-pathtest/test3 \)-prune-o-printtesttest/lo g2 01 4 .logtest/log2 01 5 .logtest/scftest/scf/libtest/scf/servicetest/scf/service/deploytest/scf/service/deploy/producttest/scf/service/deploy/infotest/scf/doctest/scf/bintest/log2 01 3 .logtest/log2 01 2 .log[root@local]#主机的描述\代表引用,它指示shell对后面的字符不做特殊解释,而是让find命令来解释其含义。
示例 3 :要搜索特定文件,请在 -o 之后添加 -name 和其他选项。
命令:复制代码如下:findtest\(-pathtest/test4 -o-pathtest/test3 \)-prune -o-name "*.log" -print 输出:复制代码如下:[root@localhostsoft]#findtes t\(-pathtest/test4 -o-pathtest/test3 \)-prune-o-name"*.log"-printtest/log2 01 4 .logtest/log2 01 5 .logtest/log2 01 3 .logtest/log2 01 2 .log[root@localhostsoft]#5 使用 user 和 nouser 选项: 按文件所有者查找文件: 示例 1 :在 $HOME 目录中查找文件所有者为 peida 的文件 命令:复制代码 代码如下:find~-userpeida-print 示例 2 :在 /etc 目录中查找 peida 文件的所有者: 命令:复制代码如下:find/etc-userpeida-print 说明: 示例 3 :按照使用的顺序查找已打印的文件:-nouser 选项。
查找 /home 目录中所有此类文件的命令: 复制代码如下: find/home-nouser-print 说明:这将允许您查找其所有者在 /etc/passwd 文件中没有有效帐户的文件。
使用-nouser选项时,不需要提供用户名; find 命令适合你。
6 、使用group和nogroup选项:就像user和nouser选项一样,find命令对于文件所属的用户组也有相同的选项。
要查找 /apps 目录中属于 gem 用户组的文件,可以使用: 复制代码 代码如下: find/apps-groupgem-print 要查找所有没有所属有效用户组的文件,可以使用 nogroup 选项。
下面的 find 命令从文件系统的根目录中搜索此类文件: 复制代码如下:find/-nogroup-print7 根据更改时间或时间搜索文件access:如果要根据修改时间查找文件,可以使用 mtime、atime 或 ctime 选项。
如果系统突然耗尽可用空间,则很可能某些文件的长度在此期间快速增长。
在这种情况下,您可以使用 mtime 选项来查找此类文件。
使用减号 - 限制从现在开始 n 天内更改的文件,使用加号 + 限制 n 天前更改的文件。
想查找系统根目录5 天内的变化吗?

Linux 中 find 命令的 35 个实际例子

下面是 Linux 下 find 命令的 3 5 个实际例子,涵盖了文件搜索、权限过滤、用户/组关联、时间间隔、大小过滤、删除操作等常见场景: 1 . 按名称查找文件 在当前目录中查找 rumenz.txt find.-namerumenz.txt 查找 rumenz.txt。
按类型查找/find/-typed-namerumenz下的rumenz目录 查找当前目录下所有PHP文件 find.-typef-name"*.php" 3 .按权限过滤 查找权限为7 7 7 的文件 find.-typef-perm07 7 7 -print 查找非7 7 7 权限的文件 find/-typef7 permission!-4 G-perm. find/-perm2 6 4 4 查找具有粘性位的文件(权限包括 5 5 1 ) find/-perm5 5 1 查找 SUID/SGID 文件 find/-perm/u=s#SUIDfind/-perm/g=s#SGID 查找只读或可执行文件 find/-perm/u=r#只读 find/-perm/sGID。
将文件权限 7 7 7 更改为 6 4 4 find/-typef-perm07 7 7 -exechmod6 4 4 {};将目录权限7 7 7 更改为7 5 5 find/-typed-perm7 7 7 -exechmod7 5 5 {}; 5 、删除文件/目录 删除单个文件 rumenz.txt find.-name "rumenz.txt" -execrm -f{};删除所有.txt或.mp3 文件 find.-name"*.txt"-execrm-f{};find.-name"*.mp3 "-execrm-f{}; 6 . 查找空文件/目录 查找 /tmp find/tmp-typef-empty 下的空文件 查找 /tmp find/tmp-typed-empty 下的空目录 7 . 查找隐藏文件 查找 /tmp find/tmp-typef-name".*" 下的所有隐藏文件 8 . 按用户/组搜索 查找/归属于 /home 下的 rumenz-ut-rumenz.t 文件 所属用户 rumenz find/home-userrumenz 查找下的文件/home 所属开发组 find/home-groupdeveloper 查找 /home 下用户 rumenz 的 .txt 文件 find/home-userrumenz-iname"*.txt" 9 . 按时间筛选 查找 5 0 天前修改的文件 find/-m 查找 5 0 天前00 find/-atime5 0 查找 5 0-1 00 天前修改的文件 find/-mtime+5 0-mtime-1 00 查找 1 小时内更改/访问的文件find/-cmin-6 0#更改状态 find/-mmin-6 0#修改内容 find/-amin-6 0#访问 1 0. 按大小过滤 5 0 MB 查找 0/-M 的文件。
5 0MB-1 00MB find/-size+5 0M-size-1 00M 删除大于1 00MB的文件 find/-typef-size+1 00M-execrm -f{};删除大于1 0MB的.mp3 文件 find/-typef-name"*.mp3 "-size+1 0M-execrm{};总结 find 命令通过结合-name、-type、-perm、-user、-mtime 等参数可以实现高效的文件查找和管理。
与-exec结合,可以直接操作结果(如删除、修改权限),方便系统维护和脚本自动化。
使用时请注意权限字段(如/requires sudo),避免误删除系统文件。