學(xué)習(xí)啦>學(xué)習(xí)電腦>操作系統(tǒng)>Linux教程>

linux中的locate命令的詳細(xì)解釋

時(shí)間: 佳洲1085 分享

  linxu下的locate命令可以在搜尋數(shù)據(jù)庫(kù)時(shí)快速找到檔案,是個(gè)很方便的命令。下面由學(xué)習(xí)啦小編為大家整理了linux的locate命令的詳細(xì)解釋的相關(guān)知識(shí),希望對(duì)大家有幫助!

  一、linux中的locate命令的詳細(xì)解釋

  1.命令格式:

  Locate [選擇參數(shù)] [樣式]

  2.命令功能:

  locate 命令可以在搜尋數(shù)據(jù)庫(kù)時(shí)快速找到檔案,數(shù)據(jù)庫(kù)由updatedb程序來(lái)更新,updatedb是由cron daemon周期性建立的,locate命令 在搜尋數(shù)據(jù)庫(kù)時(shí)比由整個(gè)由硬盤資料來(lái)搜尋資料來(lái)得快,但較差勁的是locate所找到的檔案若是最近才建立或 剛更名的,可能會(huì)找不到,在內(nèi)定值 中,updatedb每天會(huì)跑一次,可以由修改crontab來(lái)更新設(shè)定值。(etc/crontab)

  locate 指定用在搜尋符合條件的檔案,它會(huì)去儲(chǔ)存檔案與目錄名稱的數(shù)據(jù)庫(kù)內(nèi),尋找合乎范本樣式條件的檔案或目錄錄,可以使用特殊字元(如”*” 或”?”等)來(lái)指 定范本樣式,如指定范本為kcpa*ner, locate會(huì)找出所有起始字串為kcpa且結(jié)尾為ner的檔案或目錄,如名稱為kcpartner若目錄 錄名稱為kcpa_ner則會(huì)列出該目錄下包括 子目錄在內(nèi)的所有檔案。

  locate 指令和find找尋檔案的功能類似,但locate是透過(guò)update程序?qū)⒂脖P中的所有檔案和目錄資料先建立一個(gè)索引數(shù)據(jù)庫(kù),在 執(zhí)行l(wèi)oacte時(shí)直 接找該索引,查詢速度會(huì)較快,索引數(shù)據(jù)庫(kù)一般是由操作系統(tǒng)管理,但也可以直接下達(dá)update強(qiáng)迫系統(tǒng)立即修改索引數(shù)據(jù)庫(kù)。

  ps1:updatedb命令用來(lái)創(chuàng)建或更新slocate命令所必需的數(shù)據(jù)庫(kù)文件。updatedb命令的執(zhí)行過(guò)程較長(zhǎng),因?yàn)樵趫?zhí)行時(shí)它會(huì)遍歷整個(gè)系統(tǒng)的目錄樹,并將所有的文件信息寫入slocate數(shù)據(jù)庫(kù)文件中

  補(bǔ)充說(shuō)明:slocate本身具有一個(gè)數(shù)據(jù)庫(kù),里面存放了系統(tǒng)中文件與目錄的相關(guān)信息。

  updatedb命令用來(lái)創(chuàng)建或更新slocate命令所必需的數(shù)據(jù)庫(kù)文件。updatedb命令的執(zhí)行過(guò)程較長(zhǎng),因?yàn)樵趫?zhí)行時(shí)它會(huì)遍歷整個(gè)系統(tǒng)的目錄樹,并將所有的文件信息寫入slocate數(shù)據(jù)庫(kù)文件中。

  3.命令參數(shù):

  -e 將排除在尋找的范圍之外。

  -1 如果 是 1.則啟動(dòng)安全模式。在安全模式下,使用者不會(huì)看到權(quán)限無(wú)法看到 的檔案。這會(huì)始速度減慢,因?yàn)?locate 必須至實(shí)際的檔案系統(tǒng)中取得檔案的 權(quán)限資料。

  -f 將特定的檔案系統(tǒng)排除在外,例如我們沒有到理要把 proc 檔案系統(tǒng)中的檔案 放在資料庫(kù)中。

  -q 安靜模式,不會(huì)顯示任何錯(cuò)誤訊息。

  -n 至多顯示 n個(gè)輸出。

  -r 使用正規(guī)運(yùn)算式 做尋找的條件。

  -o 指定資料庫(kù)存的名稱。

  -d 指定資料庫(kù)的路徑

  -h 顯示輔助訊息

  -V 顯示程式的版本訊息

  二、Linux中的locate命令詳解實(shí)例

  實(shí)例1:查找和android相關(guān)的所有文件,并且只顯示前5個(gè)

  命令:

  location android -n 5

  輸出:

  1 aijian.shi@U-aijian-shi:~$ locate android -n 5

  2 /data/nishome/tdsw1/aijian.shi/.android

  3 /data/nishome/tdsw1/aijian.shi/.android/adbkey

  4 /data/nishome/tdsw1/aijian.shi/.android/adbkey.pub

  5 /data/nishome/tdsw1/aijian.shi/.cache/software-center/icons/android-user-es-06-icon-001-001_Portada_AU06x64.png

  6 /data/nishome/tdsw1/aijian.shi/.cache/software-center/icons/android-user-es-07-icon-icon_AU07.png

  7 aijian.shi@U-aijian-shi:~$

  實(shí)例2: 搜索alm的bin目錄下所有以i/I開頭的文件

  命令:

  locate ~/alm/bin/I* -i #-i表示不區(qū)分大小寫

  輸出:

  1 aijian.shi@U-aijian-shi:~$ locate ~/alm/bin/I* -i

  2 /data/nishome/tdsw1/aijian.shi/alm/bin/IntegrityClient

  3 /data/nishome/tdsw1/aijian.shi/alm/bin/IntegrityClient.lax

  4 /data/nishome/tdsw1/aijian.shi/alm/bin/ident

  5 /data/nishome/tdsw1/aijian.shi/alm/bin/im

  6 /data/nishome/tdsw1/aijian.shi/alm/bin/integrity

  7 aijian.shi@U-aijian-shi:~$

  *在shell中表示通配符,表示零個(gè)或多個(gè)字符。 比如當(dāng)前目錄下有abc.c、abdd.c、abeff.c。可以用ls ab*.c來(lái)查看

  注意

  ps:

  Linux手動(dòng)更新locate數(shù)據(jù)庫(kù)命令:

  updatedb

  輸出:

  1 aijian.shi@U-aijian-shi:~/Downloads$ updatedb

  2 updatedb: can not open a temporary file for `/var/lib/mlocate/mlocate.db'

  3 aijian.shi@U-aijian-shi:~/Downloads$

  4 aijian.shi@U-aijian-shi:~/Downloads$ sudo updatedb #開啟updatedb手動(dòng)更新數(shù)據(jù)庫(kù)權(quán)限

  5 [sudo] password for aijian.shi:

  6 aijian.shi@U-aijian-shi:~/Downloads$

  可以看到,我的linux機(jī)器沒有sudo權(quán)限,所以無(wú)法手動(dòng)更新數(shù)據(jù)庫(kù)。

  三、Linux中的locate命令的用法詳解

  #查看locate版本信息-V

  [plain] view plain copybixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~$ locate -V

  mlocate 0.25

  Copyright (C) 2007 Red Hat, Inc. 保留所有權(quán)利。

  本軟件在 GPL v.2 下發(fā)布。

  本程序不提供任何擔(dān)保,在法法律允許的范圍內(nèi)予以排除。

  #查看locate幫助信息

  [plain] view plain copybixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~$ locate -h

  用法: locate [OPTION]... [PATTERN]...

  在mlocate數(shù)據(jù)庫(kù)中搜索條目.

  -b, --basename 匹配唯一的路徑名稱的基本文件名

  -c, --count 只顯示找到條目的號(hào)碼

  -d, --database DBPATH 用 DBPATH 替代默認(rèn)的數(shù)據(jù)庫(kù)(/var/lib/mlocate/mlocate.db)

  -e, --existing 只顯示當(dāng)前存在的文件條目

  -L, --follow 當(dāng)文件存在時(shí)跟隨蔓延的符號(hào)鏈接 (默認(rèn))

  -h, --help 顯示本幫助

  -i, --ignore-case 匹配模式時(shí)忽略大小寫區(qū)別

  -l, --limit, -n LIMIT 限制為 LIMIT項(xiàng)目的輸出 (或 計(jì)數(shù))

  -m, --mmap 忽略向后兼容性

  -P, --nofollow, -H 當(dāng)檢查文件時(shí)不跟隨蔓延的符號(hào)

  鏈接

  -0, --null 輸出時(shí)以 NUL 分隔項(xiàng)目

  -S, --statistics 不搜索項(xiàng)目,顯示有關(guān)每個(gè)已用數(shù)據(jù)庫(kù)的統(tǒng)計(jì)信息

  -q, --quiet 不報(bào)告關(guān)于讀取數(shù)據(jù)庫(kù)的錯(cuò)誤消息

  -r, --regexp REGEXP 搜索基本正則表達(dá)式 REGEXP 來(lái)代替模式

  --regex 模式是擴(kuò)展正則表達(dá)式

  -s, --stdio 忽略向后兼容性

  -V, --version 顯示版本信息

  -w, --wholename 匹配完整路徑名 (默認(rèn))

  將 bug 報(bào)告給 mitr@redhat.com.

  # 查找etc目錄下所有以sh開頭的文件

  [plain] view plain copybixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~$ locate /etc/sh

  /etc/shadow

  /etc/shadow-

  /etc/shells

  #查看soft目錄下以android開頭的文件,限定只顯示5個(gè),使用 -n

  [plain] view plain copybixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~$ locate -n 5 /soft/android

  /home/bixiaopeng/soft/android-studio

  /home/bixiaopeng/soft/android-studio-bundle-130.737825-linux.tgz

  /home/bixiaopeng/soft/android-studio/Install-Linux-tar.txt

  /home/bixiaopeng/soft/android-studio/LICENSE.txt

  /home/bixiaopeng/soft/android-studio/NOTICE.txt

  #查找時(shí)區(qū)分大小寫

  [plain] view plain copybixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~$ locate makefile

  /usr/share/doc/python3-uno/demo/makefile.mk

  /usr/share/emacs/23.4/lisp/cedet/ede/makefile-edit.elc

  /usr/share/gtksourceview-3.0/language-specs/makefile.lang

  /usr/share/icons/Humanity/mimes/16/gnome-mime-text-x-makefile.svg

  /usr/share/icons/Humanity/mimes/22/gnome-mime-text-x-makefile.svg

  .....

  #查找時(shí)不區(qū)分大小寫,使用 -i

  [plain] view plain copybixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~$ locate -i makefile

  /usr/src/linux-headers-3.8.0-30/kernel/Makefile

  /usr/src/linux-headers-3.8.0-30/kernel/debug/Makefile

  /usr/src/linux-headers-3.8.0-30/kernel/debug/kdb/Makefile

  /usr/src/linux-headers-3.8.0-30/kernel/events/Makefile

  ......

  # ------------- 使用正規(guī)運(yùn)算式 做查找的條件 locate -r

  #查找以makefile結(jié)尾的文件

  [plain] view plain copybixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~$ locate -r makefile$

  /usr/src/linux-headers-3.8.0-19/scripts/mkmakefile

  /usr/src/linux-headers-3.8.0-19-generic/scripts/mkmakefile

  /usr/src/linux-headers-3.8.0-27/scripts/mkmakefile

  /usr/src/linux-headers-3.8.0-27-generic/scripts/mkmakefile

  /usr/src/linux-headers-3.8.0-29/scripts/mkmakefile

  /usr/src/linux-headers-3.8.0-29-generic/scripts/mkmakefile

  /usr/src/linux-headers-3.8.0-30/scripts/mkmakefile

  /usr/src/linux-headers-3.8.0-30-generic/scripts/mkmakefile

  #查找以/var/lib/dpkg/info/開頭的文件

  [plain] view plain copybixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~$ locate -r ^/var/lib/dpkg/info/

  /var/lib/dpkg/info/zlib1g:amd64.postinst

  /var/lib/dpkg/info/zlib1g:amd64.postrm

  /var/lib/dpkg/info/zlib1g:amd64.shlibs

  /var/lib/dpkg/info/zlib1g:amd64.symbols

  /var/lib/dpkg/info/zlib1g:i386.list

  /var/lib/dpkg/info/zlib1g:i386.md5sums

  /var/lib/dpkg/info/zlib1g:i386.postinst

  /var/lib/dpkg/info/zlib1g:i386.postrm

  /var/lib/dpkg/info/zlib1g:i386.shlibs

  /var/lib/dpkg/info/zlib1g:i386.symbols

3635422