zoukankan      html  css  js  c++  java
  • linux locate 文件系统快速检索


    原文链接


    part 1:   使用locate manual
    $ man locate
    locate(1)                                                     locate(1)
    NAME
           locate - find files by name
    SYNOPSIS
           locate [OPTION]... PATTERN...
    part 2: locate 的搜索机制

    whereis 严格来讲是从你环境变量的路径(path等主要路径)中查找目标。速度快,范围有限。只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。

    举例:

    $ whereis grep
    grep: /bin/grep /usr/share/man/man1/grep.1.gz
    $ whereis -b grep
    grep: /bin/grep
    $ whereis -m  grep
    grep: /usr/share/man/man1/grep.1.gz
    $ whereis -s  grep
    grep:

     

    locate则是从 updatedb命令生成的数据库中查找目标全盘文件检索 ,需要要定时运行updatedb更新数据库 ,以保证内容的实时性

    举例:

    $ locate xyz.py  # 查找一个不出在的文件 xyz.py, 找不到结果
    $ touch  xyz.py  # 手动建立xyz.py, 并
    $ locate xyz.py  # 使用 locate 查找,仍然查不到
    $ sudo updatedb  # 更新数据库
    $ locate xyz.py     # 再次locate 查找, okay
    /home/jia/xyz.py

     

    find 是“最强大,最复杂,最灵活” 的查找命令,可以用它找到任何你想找的文件

     

    which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。
    $ which find
    /usr/bin/find
    $ which cp
    /bin/cp
    $ which grep
    /bin/grep

     



    type命令其实不能算查找命令,它是用来区分某个命令到底是由shell自带的,还是由shell外部的独立二进制文件提供的。如果一个命令是外部命令,那么使用-p参数,会显示该命令的路径,相当于which命令。
    $ type cd

    cd is a shell builtin
    $ type  python
    python is /usr/bin/python
    $ type -p  python
    /usr/bin/python
    $ which python
    /usr/bin/python

     

    part 3:  OPTIONS

    param 1:
     -b, --basename
                  Match  only the base name against the specified patterns.
                  This is the opposite of --wholename.
    # 查询文件系统中文件名含有 pattern 的文件
    $ locate -b 'ue' |less
    param 2:
    -c, --count
                  Instead of writing file names on standard  output,  write
                  the number of matching entries only.
    # 查询文件系统中文件名含有 pattern 的文件的个数
    $ locate -c 'ue'

    param 3:
     -d, --database DBPATH
                  Replace  the  default  database with DBPATH.  DBPATH is a
                  :-separated list of database file names.   If  more  than
                  one --database option is specified, the resulting path is
                  a concatenation of the separate paths.


                  An empty database file name is replaced  by  the  default
                  database.   A database file name - refers to the standard
                  input.  Note that a database can be read from  the  stan鈥?
                  dard input only once.
    # 指定所以来的数据库的路径(一般使用缺省的数据库)

     

    param 4: 

    -e, --existing
                  Print  only  entries  that refer to files existing at the
                  time locate is run.
    # 显示执行locate 命令时匹配的文件列表。

    param 5:
    -L, --follow
                  When checking whether  files  exist  (if  the  --existing
                  option  is  specified),  follow  trailing symbolic links.
                  This causes broken symbolic links to be omitted from  the
                  output.
                 This is the default behavior.  The opposite can be speci鈥?
                  fied using --nofollow.
    # 统计是将链接文件一并统计在内

    param 6:
    -i, --ignore-case
                  Ignore case distinctions when matching patterns.
    # 进行文件名匹配时,不区分大小写

    param 7:
     -l, --limit, -n LIMIT
                  Exit  successfully  after  finding LIMIT entries.  If the
                  --count option is specified, the resulting count is  also
                  limited to LIMIT.
    # 进行文件名匹配时,只显示指定数目的匹配结果

     

    param 8:

    -w, --wholename

                  Match only the whole path name against the specified pat鈥?
                  terns.
                  This is the default behavior.  The opposite can be speci鈥?
                  fied using --basename.
    # 进行文件名匹配时,进行全名匹配。 即  locate -w 'ue' , 只会查找出文件名为 ue 的文件, 文件名中含有 ue 的将不会被统计出。


    用locate查找文件

    locate数据库不仅包含命令,还包含文件系统各个节点的名称,使用locate可以查找文件系统里的命令、设备、手册页、数据文件或可根据名字识别的任意文件。示例如下:

    1. $ locate e100  
    2. /lib/modules/2.6.20-1.2949.fc7/kernel/drivers/net/e100.ko  
    3. /lib/modules/2.6.20-1.2949.fc7/kernel/drivers/net/e1000/e1000.ko 

    上面的示例会查找e100.ko和e1000.ko这两个内核模块。locate命令使用-i选项时不区分大小写,否则区分大小写,示例如下:

    1. $ locate -i itco_wdt  
    2. /lib/modules/2.6.20-1.2949.fc7/kernel/drivers/char/watchdog/iTCO_wdt.ko 

    下面几个示例使用locate加上正则表达式:

    1. $ locate -r /ls$             查找以/ls$结尾的文件  
    2. /bin/ls  
    3. /usr/share/locale/l10n/ls  
    4. $ locate -r mkfs*3           查找文件名包含mkfs和3的文件  
    5. /sbin/mkfs.ext3  
    6. /usr/share/man/man8/mkfs.ext3.8.gz  
    7. $ locate -r ^/boot/grub/me   查找以/boot/grub/me开头的文件  
    8. /boot/grub/menu.lst 

    RPM包mlocate(有些Linux发行版为slocate)会提供一个cron作业,每天运行一次updatedb命令,用于更新文件locate数据库。由于用户要找的文件可能在数据库上次更新后已删除,可以使用locate -e检查数据库中找到的文件是否仍然存在:

    1. $ locate -e myfilename 

    手工运行updatedb命令可以立即更新locate数据库:

    1. # updatedb 
    【责任编辑:云霞 TEL:(010)68476606】

     

  • 相关阅读:
    [哀悼雅安芦山地震]把网页由彩色变成灰度(谷歌、火狐、ie等浏览器兼容)
    hdu 3367 Pseudoforest(最大生成树)
    解决MDK4以上版本没法对STM32软件仿真
    [置顶] java高级工程师struts的内部运行机制详解
    hdu 2141 二分搜索
    windows调试器之Visual C++
    2013年4月19日佳都新太笔试题+解答
    一步步理解Linux进程(3)内核中进程的实现
    什么是你的核心竞争力之一?
    ubuntu创建桌面启动器
  • 原文地址:https://www.cnblogs.com/wangkangluo1/p/2563371.html
Copyright © 2011-2022 走看看