萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 文件搜尋入門:find、locate、whereis、which 四大工具詳解

Linux 檔案搜尋主要分兩類:依檔名定位檔案位置、於檔案內容搜尋關鍵字。本文聚焦前者,介紹四大常用工具。

依檔名查找檔案

1. find:最強大靈活

基本語法:find 路徑 -name 檔名

範例: 在 /etc 找 vsftpd.conf:

[root@localhost ~]# find /etc -name vsftpd.conf

進階用法請參考《Linux 文件查找命令 find,xargs 詳述》。

2. locate:快速索引搜尋

透過預建資料庫快速定位,使用前須建立/更新索引:

[root@localhost ~]# updatedb

新安裝軟體或新增檔案後務必先執行 updatedb。

範例: 搜尋 vsftpd.conf 所有位置:

[root@localhost ~]# locate vsftpd.conf

輸出可能包含:
/etc/vsftpd/vsftpd.conf
/home/beinan/vsftpd.conf
/root/.vsftpd.conf.swp
/usr/share/doc/vsftpd-2.0.4/EXAMPLE/INTERNET_SITE/vsftpd.conf 等多路徑。

3. whereis 與 which:尋找命令路徑

3.1 whereis

尋找命令二進位檔與對應 man page。

[root@localhost ~]# whereis fdisk
fdisk: /sbin/fdisk /usr/share/man/man8/fdisk.8.gz
[root@localhost ~]# whereis grub
grub: /sbin/grub /etc/grub.conf /usr/share/grub /usr/share/man/man8/grub.8.gz

可知 grub 執行檔在 /sbin,man page 為 grub.8.gz,執行 man grub 即可閱讀。

3.2 which

僅在 PATH 環境變數定義路徑中搜尋命令位置。

[root@localhost ~]# which fdisk
/sbin/fdisk
copyright © 萬盛學電腦網 all rights reserved