ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux复习——文件查找命令

2021-12-27 12:31:20  阅读:120  来源: 互联网

标签:文件 复习 ifconfig etc 查找 usr Linux root find


文件查找命令

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

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

标签:文件,复习,ifconfig,etc,查找,usr,Linux,root,find
来源: https://blog.csdn.net/weixin_45740510/article/details/122169186

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有