当前位置:首页 » 《随便一记》 » 正文

Linux find命令教程:快速高效地搜索文件和目录(附实例详解和注意事项)

1 人参与  2024年09月17日 15:20  分类 : 《随便一记》  评论

点击全文阅读


Linux find 命令介绍

find,全称 File IN Databank,是一个强大的文件搜索命令,可以根据各种标准如文件名、大小、修改日期等在系统中搜索文件和目录。

Linux find 命令适用的Linux版本

find 这个命令适用于所有Linux版本,包括但不限于 Ubuntu、Debian、Fedora、Red Hat、CentOS 等。由于 find 是预安装在所有Linux发行版上的,所以大部分时候并不需要进行额外的安装。

Linux find 命令的基本语法

基本的命令语法如下:

find [pathname] [expression]

Linux find命令的常用选项或参数说明

参数/选项说明
-name [file]根据文件名搜索文件
-size [size]根据文件大小搜索文件
-type [type]根据类型搜索文件,如 -type d 为搜索目录
-mtime [n]根据修改时间搜索文件,n为天数
-exec [command] {} ;将查找的文件执行指定的命令

Linux find命令实例详解

实例1:查找名称为filename的文件

[linux@bashcommandnotfound.cn ~]$ find / -name filename

实例2:搜索所有大于1MB的文件

[linux@bashcommandnotfound.cn ~]$ find / -size +1M

实例3:查找当前目录及子目录下所有的 .txt 文件

[linux@bashcommandnotfound.cn ~]$ find . -name "*.txt"

实例4:在/home目录中查找以.log结尾的文件

[linux@bashcommandnotfound.cn ~]$ find /home -name "*.log"

实例5:查找/home目录中用户所有者为root,并且文件大小超过10M的文件

[linux@bashcommandnotfound.cn ~]$ find /home -user root -size +10M

实例6:在当前目录查找更改时间在5天之内的文件

[linux@bashcommandnotfound.cn ~]$ find . -mtime -5

实例7:找出所有空的目录

[linux@bashcommandnotfound.cn ~]$ find / -empty -type d

实例8:在/var/log目录下查找最近5分钟内修改过的文件

[linux@bashcommandnotfound.cn ~]$ find /var/log -mmin -5

实例9:查找并删除所有的 .tmp 文件

这是一个示例,但请小心使用,不要坐数组。

[linux@bashcommandnotfound.cn ~]$ find / -name "*.tmp" -exec rm {} \;

实例10:查找并打印所有以 .sh 结尾的文件,并显示其文件大小和最后修改时间

[linux@bashcommandnotfound.cn ~]$ find / -name "*.sh" -exec ls -lh {} \;

实例11:查找当前目录及子目录中文件权限为755的文件

[linux@bashcommandnotfound.cn ~]$ find . -type f -perm 0755

实例12:在/home目录中查找拥有者为root的文件

[linux@bashcommandnotfound.cn ~]$ find /home -owner root

实例13:查找/home目录中,更改时间在10天前,15天内的文件

[linux@bashcommandnotfound.cn ~]$ find /home -mtime +10 -mtime -15

实例14:查找/etc目录下,更改时间超过10天,并且后缀为’.log’的文件

[linux@bashcommandnotfound.cn ~]$ find /etc -name "*.log" -mtime +10

实例15:查找/var/log目录下,10天内被访问过,并且后缀为’.log’的文件

[linux@bashcommandnotfound.cn ~]$ find /var/log -name "*.log" -atime -10

Linux find 命令的注意事项

搜索根目录(/)时记得使用 -sudo 选项,否则有些文件由于权限问题可能无法搜索到。使用文件路径进行搜索时,建议使用绝对路径,这样可以避免因路径错误而不能正确找到文件的问题。bash: find: command not found, 如果发生这种情况,请按照上面的方法安装。

Linux find 相关命令

locate命令:在Linux系统中更快的查找文件grep命令:基于文本内容的搜索which命令:在用户的PATH中确定执行文件位置whereis命令:查找二进制文件、源文件ls命令:列出当前目录下的文件cp命令:在Linux中复制文件或目录mv命令:在Linux中移动或重命名文件rm命令:在Linux中删除文件和目录

点击全文阅读


本文链接:http://zhangshiyu.com/post/160950.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1