Linux文件查找命令(find locate whereis which)

本文详细介绍了Linux系统中的四个常用文件查找命令find、locate、whereis和which的功能、用法示例,以及它们在效率和适用场景上的差异。通过实例了解如何高效搜索文件、文件类型、时间、权限和综合运用这些工具。
摘要由CSDN通过智能技术生成

文件查找命令

find locate whereis which

1、find: 在Linux系统中用来进行文件搜索用的最多的命令,功能特别强大。但是尽量少用find命令去执行搜索任务,就算要搜索也应该尽量缩小范围,且不要在服务器使用高峰期进行文件搜索,因为搜索也是很占系统资源的。

  • 根据文件或目录搜索:

    • find / -name passwd 查找根目录/ 下叫passwd的文件
      在这里插入图片描述
    • find /etc -name “*.sh” 查找/etc下以 .sh 结尾的文件
    • find /etc -name “host*” 查找/etc下以 host开头的文件
    • find /etc -iname abc 忽略大小写字母查询
  • 根据文件大小搜索

    • 常见大小单位

      c:字节

      k:Kb

      M:Mb

      G:Gb

    • find /root -size +50k 查找/root目录下大于50 kb 的文件

      find /root -size -50k - 是小于

      find /root -size 50k 等于

      find /root -size +50k -size -100k

      查找/root目录下大于50 kb 并且小于100kb 的文件

  • 根据文件类型搜索

    • 文件类型

    • find /etc -type f

      f:普通文件

      d:目录文件

      l:链接文件

      b:块设备文件

      c:字符设备文件

      p:管道文件

      s:套接字文件

    • find / -type f

      find / -type f -name “*.txt”

      find /usr -type d -name “unix*”

      find / -type d -empty

      find /dev -type b

      find /etc -type d

  • 根据时间搜索

    • 时间参数

      • 修改时间(mtime):最后一次文件内容有过更 改的时间点
      • 访问时间(atime):最后一次文件有被读取过 的时间点
      • 变更时间(ctime):最后一次文件有被变更过 的时间点,包括内容更改、权限更改等
    • find /usr -type f -mtime 2

      /usr目录中两天前被修改过的文件

      find /usr -type f -mtime +50 -mtime -100

      /usr下50到100天之前修改过的文件

      find /usr -type f -mtime 2 -atime 1

      两天前被修改过且1天前被访问过的文件

      find /usr -type f -mmin 5 -amin 2

      5分钟前被修改过且2分钟前被访问过的文件

      find /root -cmin -30

      30分钟内被变更过的文件

      find / -newer log1 ! -newer log2

      更改时间比log1新,比log2旧的文件

  • 根据所属权限搜索

    • fifind /usr -perm u=rwx,g-rx,o=r

      fifind /usr -perm 644

      fifind / -user “nickel”

      fifind / -uid “1001”

      fifind / -group “test”

      fifind / -gid “1002”

  • 综合搜索

    • 反向搜索

    • 忽略某个文件或目录

    • 限制目录的遍历成熟

    • 逻辑组合and、or

    • 对搜索结果执行命令

      • 删除:-delete
      • 执行:-exec
      • 显示:-ls
    • 查找当前目录及子目录下大于1G的文件:

      # find ./ -size +1G -exec ls -lh {} \;
      

2、locate :在后台数据库中按文件名全盘搜索,速度比find 快得多,但是对于刚创建的文件还没有被收录到数据库,可能搜不到。

locate 的使用

[root@localhost etc]# locate inittab
-bash: locate: command not found      # 系统上没有安装locate
[root@localhost etc]# yum install -y mlocate  # 安装mlocate包
[root@localhost etc]# locate inittab      # 查找inittab这个文件
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory # 文件数据库未建立
[root@localhost etc]# **updatedb**     # 手动生成文件数据库
[root@localhost etc]# locate inittab  # 可以看到此时文件数据库已生成,查找到包含inittab字符串的文件

在这里插入图片描述
3、whereis

在数据库中搜索二进制文件(-b)、man说明文件(-m)和源代码文件(-s)

whereis [-bmsu] 文件或者目录名

-b: 只找二进制格式的文件

-m: 只找在说明文件manual 路劲下的文件

-s: 只找source 源文件

-u: 查找不在上述三个选项中的其他特殊文件

查询ifconfig 文件名

[root@localhost etc]# whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[root@localhost etc]# whereis -b ifconfig
ifconfig: /usr/sbin/ifconfig
[root@localhost etc]# whereis -m ifconfig
ifconfig: /usr/share/man/man8/ifconfig.8.gz
[root@localhost etc]# whereis -s ifconfig
ifconfig:

4、which

用于查找命令是否存在,以及命令的存放位置
在这里插入图片描述

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值