在Linux上尋找你正在尋找的東西
怎樣在 Linux 系統(tǒng)上使用 find、locate、mlocate、which、 whereis、 whatis 和 apropos 命令尋找文件。
在 Linux 系統(tǒng)上找到你要找的文件或命令并不難, 有很多種方法可以尋找。
find
最顯然的無(wú)疑是 find 命令,并且 find 變得比過(guò)去幾年更容易使用了。它過(guò)去需要一個(gè)搜索的起始位置,但是現(xiàn)在,如果你想將搜索限制在當(dāng)下目錄中,你還可以使用僅包含文件名或正則表達(dá)式的 find 命令。
$ find e*emptyexamples.desktop
這樣,它就像 ls 命令一樣工作,并沒(méi)有做太多的搜索。
對(duì)于更專業(yè)的搜索,find 命令需要一個(gè)起點(diǎn)和一些搜索條件(除非你只是希望它提供該起點(diǎn)目錄的遞歸列表)。命令 find -type f 從當(dāng)前目錄開始將遞歸列出所有常規(guī)文件,而 find ~nemo -type f -empty 將在 nemo 的主目錄中找到空文件。
$ find ~nemo -type f -empty/home/nemo/empty
locate
locate 命令的名稱表明它與 find 命令基本相同,但它的工作原理完全不同。find 命令可以根據(jù)各種條件 —— 名稱、大小、所有者、權(quán)限、狀態(tài)(如空文件)等等選擇文件并作為搜索選擇深度,locate 命令通過(guò)名為 /var/lib/mlocate/mlocate.db 的文件查找你要查找的內(nèi)容。該數(shù)據(jù)文件會(huì)定期更新,因此你剛創(chuàng)建的文件的位置它可能無(wú)法找到。如果這讓你感到困擾,你可以運(yùn)行 updatedb 命令立即獲得更新。
$ sudo updatedb
mlocate
mlocate 命令的工作類似于 locate 命令,它使用與 locate 相同的 mlocate.db 文件。
which
which 命令的工作方式與 find 命令和 locate 命令有很大的區(qū)別。它使用你的搜索路徑($PATH)并檢查其上的每個(gè)目錄中具有你要查找的文件名的可執(zhí)行文件。一旦找到一個(gè),它會(huì)停止搜索并顯示該可執(zhí)行文件的完整路徑。
which 命令的主要優(yōu)點(diǎn)是它回答了“如果我輸入此命令,將運(yùn)行什么可執(zhí)行文件?”的問(wèn)題。它會(huì)忽略不可執(zhí)行文件,并且不會(huì)列出系統(tǒng)上帶有該名稱的所有可執(zhí)行文件 —— 列出的就是它找到的第一個(gè)。如果你想查找具有某個(gè)名稱的所有可執(zhí)行文件,則可以像這樣運(yùn)行 find 命令,但是要比非常高效 which 命令用更長(zhǎng)的時(shí)間。
$ find / -name locate -perm -a=x 2>/dev/null/usr/bin/locate/etc/alternatives/locate
在這個(gè) find 命令中,我們?cè)趯ふ颐麨?“locate” 的所有可執(zhí)行文件(任何人都可以運(yùn)行的文件)。我們也選擇了不要查看所有“拒絕訪問(wèn)”的消息,否則這些消息會(huì)混亂我們的屏幕。
whereis
whereis 命令與 which 命令非常類似,但它提供了更多信息。它不僅僅是尋找可執(zhí)行文件,它還尋找手冊(cè)頁(yè)(man page)和源文件。像 which 命令一樣,它使用搜索路徑($PATH) 來(lái)驅(qū)動(dòng)搜索。
$ whereis locatelocate: /usr/bin/locate /usr/share/man/man1/locate.1.gz
whatis
whatis 命令有其獨(dú)特的使命。它不是實(shí)際查找文件,而是在手冊(cè)頁(yè)中查找有關(guān)所詢問(wèn)命令的信息,并從手冊(cè)頁(yè)的頂部提供該命令的簡(jiǎn)要說(shuō)明。
$ whatis locatelocate (1) - find files by name
如果你詢問(wèn)你剛剛設(shè)置的腳本,它不會(huì)知道你指的是什么,并會(huì)告訴你。
$ whatis cleanupcleanup: nothing appropriate.
apropos
當(dāng)你知道你想要做什么,但不知道應(yīng)該使用什么命令來(lái)執(zhí)行此操作時(shí),apropos 命令很有用。例如,如果你想知道如何查找文件,那么 apropos find 和 apropos locate 會(huì)提供很多建議。
$ apropos findFile::IconTheme (3pm) - find icon directoriesFile::MimeInfo::Applications (3pm) - Find programs to open a file by mimetypeFile::UserDirs (3pm) - find extra media and documents directoriesfind (1) - search for files in a directory hierarchyfindfs (8) - find a filesystem by label or UUIDfindmnt (8) - find a filesystemgst-typefind-1.0 (1) - print Media type of fileippfind (1) - find internet printing protocol printerslocate (1) - find files by namemlocate (1) - find files by namepidof (8) - find the process ID of a running program.sane-find-scanner (1) - find SCSI and USB scanners and their device filessystemd-delta (1) - Find overridden configuration filesxdg-user-dir (1) - Find an XDG user dir$$ apropos locateblkid (8) - locate/print block device attributesdeallocvt (1) - deallocate unused virtual consolesfallocate (1) - preallocate or deallocate space to a fileIO::Tty (3pm) - Low-level allocate a pseudo-Tty, import constants.locate (1) - find files by namemlocate (1) - find files by namemlocate.db (5) - a mlocate databasemshowfat (1) - shows FAT clusters allocated to filentfsfallocate (8) - preallocate space to a file on an NTFS volumesystemd-sysusers (8) - Allocate system users and groupssystemd-sysusers.service (8) - Allocate system users and groupsupdatedb (8) - update a database for mlocateupdatedb.mlocate (8) - update a database for mlocatewhereis (1) - locate the binary, source, and manual page files for a...which (1) - locate a command
總結(jié)
Linux 上可用于查找和識(shí)別文件的命令有很多種,但它們都非常有用。





















