linux 下查找文件及完备文件路径

打印 上一主题 下一主题

主题 901|帖子 901|积分 2703

一、指令文件的的搜寻

我们知道在终端机模式当中,连续输入两次[tab]按键就能够知道使用者有多少指令可以下达。 那你知不知道这些指令的完备文件名放在那里?举例来说,ls 这个常用的指令放在那里呢? 就通过 which 或 type 来找寻吧!


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

再来谈一谈怎么搜寻文件吧!在 Linux 下面也有相当优秀的搜寻指令呦!通常 find 不很常用的!由于速度慢之外, 也很操硬盘!一般我们都是先使用 whereis ,假如真的找不到了,才以 find 来搜寻呦! 为什么呢?由于 whereis 只找系统中某些特定目次下面的文件而已,locate 则是使用数据库来搜寻文件名,当然两者就相当的快速, 而且没有现实的搜寻硬盘内的文件系统状态,比力省时间啦!
1、whereis (由一些特定的目次中探求文件文件名)

  1. [root@study ~]# whereis [-bmsu] 文件或目录名
  2. 选项与参数:
  3. -l    :可以列出 whereis 会去查询的几个主要目录而已
  4. -b    :只找 binary 格式的文件
  5. -m    :只找在说明文档 manual 路径下的文件
  6. -s    :只找 source 来源文件
  7. -u    :搜寻不在上述三个项目当中的其他特殊文件
  8. 范例一:请找出 ifconfig 这个文件名
  9. [root@study ~]# whereis ifconfig
  10. 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选项
                    
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
  1. [root@localhost etc]# find /etc/ -type d |more
  2. /etc/
  3. /etc/fonts
  4. /etc/fonts/conf.d
  5. /etc/grub.d
  6. /etc/pki
  7. /etc/pki/rpm-gpg
  8. /etc/pki/ca-trust
  9. /etc/pki/ca-trust/extracted
  10. /etc/pki/ca-trust/extracted/java
  11. /etc/pki/ca-trust/extracted/openssl
  12. /etc/pki/ca-trust/extracted/pem
  13. /etc/pki/ca-trust/source
  14. /etc/pki/ca-trust/source/anchors
  15. /etc/pki/ca-trust/source/blacklist
  16. /etc/pki/java
  17. /etc/pki/tls
  18. /etc/pki/tls/certs
复制代码
2.3按照文件大小查找

通项:find 路径 -size 范围
范围:+表现大于        -表现小于        等于不必要符号
大小:M(必须大写)        k(必须小写)        c(表现字符)
查找该目次下0k到10k的文件
   [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 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层)
  1. 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 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 . -mindepth 2
./conf.d/default.conf.rpmsave
./conf.d/default.conf
#就相当于是 目次以下的文件列出来
  
3、grep命令

即按照内容查找
  1. grep 参数 "内容" 查找目录或文件
复制代码
参数
-r 若是目次,则可以递归查找
-n:可以表现该查找内容所在的行号
-i:可以忽略大小写进行查找
-v:不表现含有某字符串


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

傲渊山岳

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表