萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Linux 常用搜尋指令大全:find、locate、whereis、which、type

1. find — 最強大的檔案搜尋工具

語法:find <指定目錄> <指定條件> <指定動作>

常用條件參數

參數說明
-name 模式依檔名搜尋,支援萬用字元 * ?
-type x依檔案類型搜尋(f=一般檔、d=目錄、l=符號連結等)
-size n依大小搜尋(c=bytes、k=KB)
-mtime nn 天前修改過的檔案
-mmin nn 分鐘前修改過的檔案
-user name依擁有者搜尋
-perm mode依權限搜尋

實用範例

# 找出當前目錄下所有以 my 開頭的檔案
find . -name 'my*'

# 找出 10 分鐘內修改過的一般檔案
find . -type f -mmin -10

# 找出 /etc 下大小小於 2500 bytes 的檔案
find /etc -size -2500c -print

# 找出系統中名為 httpd.conf 的檔案
find / -name httpd.conf -print

2. locate — 快速資料庫搜尋

透過預建資料庫(/var/lib/locatedb)搜尋,速度極快,但非即時。需先執行 updatedb 更新資料庫。

locate /etc/sh        # 搜尋 /etc 下以 sh 開頭的檔案
locate -i ~/m         # 忽略大小寫搜尋家目錄下以 m 開頭的檔案

3. whereis — 程式二進制、原始碼、手冊頁搜尋

whereis grep
# 參數:-b 二進制、-m 手冊、-s 原始碼

4. which — PATH 中指令的實際路徑

which grep

5. type — 判斷指令來源

type cd       # 顯示 shell 內建指令
type grep     # 顯示外部指令及路徑
type -p grep  # 相當於 which grep

總結

  • 找檔案用 find(即時、條件豐富)
  • 找指令路徑用 which 或 type -p
  • 快速模糊搜尋用 locate(記得先 updatedb)
  • 查詢指令屬性用 type
copyright © 萬盛學電腦網 all rights reserved