开发者

linux下查找文件及完整文件路径实现方式

目录
  • 一、指令文件的的搜寻
    • which (寻找“可执行文件”)
  • 二、文件文件名的搜寻
    • 1、whereis (由一些特定的目录中寻找文件文件名)
    • 2、fing命令
      • 2.1、按文件名
      • 2.2按文件类型查询
      • 2.3按照文件大小查找
      • 2.4按照文件日期查找
      • 2.5按深度查找
    • 3、grep命令
    • 总结

      一、指令文件的的搜寻编程

      我们知道在终端机模式当中,连续输入两次[tab]按键就能够知道使用者有多少指令可以下达。 那你知不知道这些指令的完整文件名放在哪里?

      举例来说,ls 这个常用的指令放在哪里呢? 就通过 which 或 type 来找寻吧!

      which (寻找“可执行文件”)

      [root@study ~]# which [-a] command
      选项或参数:
      -a :将所有由 PATH 目录中可以找到的指令均列出,而不止第一个被找到的指令名称
      
      范例一:搜寻 ifconfig 这个指令的完整文件名
      [root@study ~]# which ifconfig
      /sbin/ifconfig 
      
      范例二:用 which 去找出 which 的文件名为何?
      [root@study ~]# which which
      alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
              /bin/alias
              /usr/bin/which
      # 竟然会有两个 which ,其中一个是 alias 这玩意儿呢!那是啥?
      # 那就是所谓的“命令别名”,意思是输入 which 会等于后面接的那串指令啦!
      # 更多的数据我们会在 bash 章节中再来谈的!
      
      范例三:请找出 history 这个指令的完整文件名
      [root@study ~]# which history
      /usr/bin/which: no history in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:
      /usr/sbin:/usr/bin:/root/bin)
      
      [root@study ~]# history --help
      -bash: history: --: invalid option
      history: usage: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg 
      # 瞎密?怎么可能没有 history ,我明明就能够用 root 执行 history 的啊!

      二、文件文件名的搜寻

      再来谈一谈怎么搜寻文件吧!在 linux 下面也有相当优异的搜寻指令呦!通常 find 不很常用的!因为速度慢之外, 也很操硬盘!

      一般我们都是先使用 whereis ,如果真的找不到了,才以 find 来搜寻呦!

      为什么呢?因为 whereis 只找系统中某些特定目录下面的文件而已,locate 则是利用数据库来搜寻文件名,当然两者就相当的快速, 并且没有实际的搜寻硬盘内的文件系统状态,比较省时间啦!

      1、whereis (由一些特定的目录中寻找文件文件名)

      [root@study ~]# whereis [-bmsu] 文件或目录名
      选项与参数:
      -l    :可以列出 whereis 会去查询的几个主要目录而已
      -b    :只找 binary 格式的文件
      -m    :只找在说明文档 manual 路径下的文件
      -s    :只找 source 来源文件
      -u    :搜寻不在上述三个项目当中的其他特殊文件
      
      范例一:请找出 ifconfig 这个文件名
      [root@study ~]# whereis ifconfig 
      ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
      

      等一下我们会提到 find 这个搜寻指令, find 是很强大的搜寻指令,但时间花用的很大!(因为 find 是直接搜寻硬盘,为如果你的硬盘比较老旧的话,嘿嘿!有的等!) 这个时候 whereis 就相当的好用了!另外, whereis 可以加入选项来找寻相关的数据,例如,如果你是要找可可执行文件 (binary) 那么加上 -b 就可以啦! 如果不加任何选项的话,那么就将所有的数据列出来啰!

      那么 whereis 到底是使用什么咚咚呢?为何搜寻的速度会比 find 快这么多? 其实那也没有什么,只是因为 whereis 只找几个特定的目录而已~并没有全系统去查询之故。所以说,whereis 主要是针对 /bin /sbin 下面的可执行文件, 以及 /usr/share/man 下面的 man page 文件,跟几个比较特定的目录来处理而已。所以速度当然快的多!不过,就有某些文件是你找不到的啦! 想要知道 whereis 到底查了多少目录?可以使用 whereis -l 来确认一下即可!

      2、fing命令

      选项描述
      -name按照文件名搜索(区分大小)
      -type按照文件类型搜索
      -size按照文件大小搜索
      -mtime按照文件修改时间搜索
      -exec对搜索结果执行指定命令
      -maxdepth设置搜索深度
      -mindepth设置搜索深度
      -user按照文件所有者搜索
      -group按照文件所属组搜索
      -perm按照文件权限搜索
      -print将搜索结果输出到标准输出
      -delete删除搜索结果
      -ok和 - exec 类似,但需要确认
      -iname按照文件名搜索(不区分大小写)
      -printf按照指定格式输出搜索结果
      -regex按照正则表达式搜索
      -newer按照文件修改时间搜索
      -depth从深度优先搜索改为广度优先搜索
      -mount只搜索当前挂载的文件系统
      -xdev同 - mound 选项(注:原文可能存在拼写误差,标准选项为 - mount,-xdev 功能与之类似,均用于限制在当前文件系统搜索)

      2.1、按文件名

      查找文件/etc  下所有以conf为结尾的文件

      [root@localhost etc]# find  /etc/ -name "*.conf"
      /etc/resolv.conf
      /etc/fonts/conf.d/65-0-khmeros-base.conf
      /etc/fonts/conf.d/61-urw-fallback-backwards.conf
      /etc/fonts/conf.d/66-ucs-miscfixed.conf
      /etc/fonts/conf.d/31-cantarell.conf
      /etc/fonts/conf.d/60-overpass.conf
      /etc/fonts/conf.d/59-liberation-sans.conf
      /etc/fonts/conf.d/65-0-lohit-devanagari.conf
      /etc/fonts/conf.d/20-unhint-small-dejavu-sans.conf
      /etc/fonts/conf.d/65-sil-padauk.conf
      /etc/fonts/conf.d/57-dejavu-sans.conf
      /etc/fonts/conf.d/65-0-lohit-kannada.conf
      /etc/fonts/conf.d/59-liberation-mono.conf
      /etc/fonts/conf.d/65-0-nhn-nanum-gothic.conf
      /etc/fonts/conf.d/10-hinting-slight.conf
      /etc/fonts/conf.d/66-sil-abyssinica.conf
      /etc/fonts/conf.d/10-scale-bitmap-fonts.conf
      /etc/fonts/conf.d/66-sil-nuosu.conf
      /etc/fonts/conf.d/20-unhint-small-vera.conf
      /etc/fonts/conf.d/65-0-ttf-arphic-uming.conf
      /etc/fonts/conf.d/25-no-bitmap-Fedora.conf
      /etc/fonts/conf.d/90-ttf-arphic-uming-embolden.conf
      /etc/fonts/conf.d/25-unhint-nonlatin.conf
      /etc/fonts/conf.d/65-0-lohit-tamil.conf
      /etc/fonts/conf.d/30-metric-aliases.conf
      /etc/fonts/conf.d/69-gnu-free-sans.conf
      /etc/fonts/conf.d/30-urw-aliases.conf
      /etc/fonts/conf.d/69-gnu-free-serif.conf
      /etc/fonts/conf.d/40-nonlatin.conf

      2.2按文件类型查询

      find 路径 -type 类型

      类型

      普通文件 f        目录d        符号链接l        块设备文件b

      字符设备文件c        socket文件s        管道文件p

      [root@localhost etc]# find /etc/ -type d |more
      /etc/
      /etc/fonts
      /etc/fonts/conf.d
      /etc/grub.d
      /etc/pki
      /etc/pki/rpm-gpg
      /etc/pki/ca-trust
      /etc/pki/ca-trust/extracted
      /etc/pki/ca-trust/extracted/Java
      /etc/pki/ca-trust/extracted/openssl
      /etc/pki/ca-trust/extracted/pem
      /etc/pki/ca-trust/source
      /etc/pki/ca-trust/source/anchors
      /etc/pki/ca-trust/source/blacklist
      /etc/pki/java
      /etc/pki/tls
      /etc/pki/tls/certs

      2.3按照文件大小查找

      通项:find 路径 -size 范围

      范围:+表示大于        -表示小于        等于不需要符号

      大小:Mandroid(必须大写)        k(必须小写)        c(表示字符)

      查找该目录下0k到10k的文件

      [root@localhost nginx]# ll
      总用javascript量 28
      drwxr-xr-x. 2 root root   54 6月  29 01:55 conf.d
      -rw-r--r--. 1 root root 1007 5月  30 03:07 fastcgi_params
      -rw-r--r--. 1 root root 5349 5月  30 03:07 mime.types
      lrwxrwxrwx. 1 root root   29 6月  29 01:41 modules -> ../../usr/lib64/nginx/modules
      -rw-r--r--. 1 root root  918 6月  29 04:05 nginx.conf
      -rw-r--r--. 1 root root  632 6月  29 01:32 nginx.conf.rpmsave
      -rw-r--r--. 1 root root  636 5月  30 03:07 scgi_params
      -rw-r--r--. 1 root root  664 5月  30 03:07 uwsgi_params
      [root@localhost nginx]# find /etc/nginx/ -size +0  -size -10 
      /etc/nginx/
      /etc/nginx/conf.d
      /etc/nginx/conf.d/default.conf.rpmsave
      /etc/nginx/conf.d/default.conf
      /etc/nginx/nginx.conf.rpmsave
      /etc/nginx/fastcgi_params
      /etc/nginx/modules
      /etc/nginx/scgi_params
      /etc/nginx/uwsgi_params
      /etc/nginx/nginx.conf

      2.4按照文件日期查找

      2.4.1 按照创建日期查找 Ctime

      find 路径 -ctime -n/+n

      [root@localhost nginx]# find /etc/nginx/ -ctime -7
      /etc/nginx/
      /etc/nginx/conf.d
      /etc/nginx/conf.d/default.conf.rpmsave
      /etc/nginx/conf.d/default.conf
      /etc/nginx/nginx.conf.rpmsave
      /etc/nginx/fastcgi_params
      /etc/nginx/mime.types
      /etc/nginx/modules
      /etc/nginx/scgi_params
      /etc/nginx/uwsgi_params
      /etc/nginx/.nginx.conf.swp
      /etc/nginx/nginx.conf
      /etc/nginx/KIND
      /etc/nginx/kdw

      2.4.2按照修改日期查找 mtime

      find 路径 -mtime -n/+n

       [root@localhost nginx]# find . -mtime -3 
      .
      ./KIND
      ./kdw
      [root@localhost nginx]# ls -la

      2.4.3按照访问日期查找 atime

      find 路径 -atime -n/+n

      [root@localhost nginx]# find . -atime -1
      .
      ./conf.d
      ./conf.d/default.conf
      ./mime.types
      ./modules
      ./nginx.conf
      ./KIND
      ./kdw

      2.5按深度查找

      2.5.1查找起始点以下n层的目录,不超过n层 (就是从当前目录的上一级开始查找)

      find 路径 -maxdepth n(层数)

      [root@localhost ~]# cd /etc/nginx/
      [root@localhost nginx]# find . -maxdepth 2
      .
      ./conf.d
      ./conf.d/default.conf.rpmsave
      ./conf.d/default.conf
      ./nginx.conf.rpmsave
      ./fastcgi_params
      ./mime.types
      ./modules
      ./scgi_params
      ./uwsgi_params
      ./.nginx.conf.swp
      ./nginx.conf
      #就相当于在nginx 以下的文件及连接等都列出来

      搜距离起始点n层以下的目录(即最少n层)

      find 路径 -mindepth n(层数)
      [root@localhost nginx]# ll
      总用量 28
      drwxr-xr-x. 2 root root   54 6月  29 01:55 conf.d
      -rw-r--r--. 1 root root 1007 5月  30 03:07 fastcgi_params
      -rw-r--r--. 1 root root 5349 5月  30 03:07 mime.types
      lrwxrwxrwx. 1 root root   29 6月  29 01:41 modules -> .编程./../usr/lib64/nginx/modules
      -rw-r--r--. 1 root root  918 6http://www.devze.com月  29 04:05 nginx.conf
      -rw-r--r--. 1 root root  632 6月  29 01:32 nginx.conf.rpmsave
      -rw-r--r--. 1 root root  636 5月  30 03:07 scgi_params
      -rw-r--r--. 1 root root  664 5月  30 03:07 uwsgi_params
      
      
      [root@localhost nginx]# find . -mindepth 2
      ./conf.d/default.conf.rpmsave
      ./conf.d/default.conf
      #就相当于是 目录以下的文件列出来

      3、grep命令

      即按照内容查找

      grep 参数 "内容" 查找目录或文件

      参数

      • -r 若是目录,则可以递归查找
      • -n:可以显示该查找内容所在的行号
      • -i:可以忽略大小写进行查找
      • -v:不显示含有某字符串

      总结

      以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

      0

      上一篇:

      下一篇:

      精彩评论

      暂无评论...
      验证码 换一张
      取 消

      最新运维

      运维排行榜