Linux 檔案搜尋主要分兩類:依檔名定位檔案位置、於檔案內容搜尋關鍵字。本文聚焦前者,介紹四大常用工具。
基本語法:find 路徑 -name 檔名
範例: 在 /etc 找 vsftpd.conf:
[root@localhost ~]# find /etc -name vsftpd.conf
進階用法請參考《Linux 文件查找命令 find,xargs 詳述》。
透過預建資料庫快速定位,使用前須建立/更新索引:
[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 等多路徑。
尋找命令二進位檔與對應 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 即可閱讀。
僅在 PATH 環境變數定義路徑中搜尋命令位置。
[root@localhost ~]# which fdisk
/sbin/fdisk