linux locate 檔案系統快速檢索

來源:互聯網
上載者:User
  • locate 命令
  • 51cto

 

原文連結


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加上Regex:

 

  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】

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.