查找文件
一般查找find
最常用的方式find PATH -name FILENAME.例如查找當前用戶目錄下的1.txt文件,find ~ -name 1.txt.同時還可以使用*號匹配.如果想忽略文件名的大小寫,可以添加-iname參數(shù).
[zengchao@localhost dir]$ tree
.
├── 1.txt
├── 2.txt
├── A.doc
├── b.doc
└── child
├── c1.txt
└── c2.doc
3 directories, 4 files
[zengchao@localhost dir]$ find ./ -name 1.txt
./1.txt
[zengchao@localhost dir]$ find ./ -name a.doc
[zengchao@localhost dir]$ find ./ -iname a.doc
./A.doc
[zengchao@localhost dir]$ find ./ -name '*.txt'
./1.txt
./2.txt
./child/c1.txt
數(shù)據(jù)庫查找:locate
locate命令查找數(shù)據(jù)會依賴本地的數(shù)據(jù)庫文件,默認每天會檢索一遍系統(tǒng)中的文件,然后記錄到數(shù)據(jù)庫中.也可以使用命令updatedb手動更新數(shù)據(jù)庫文件.例如下面的操作:
[zengchao@localhost ~]$ touch zengchao_test.file
[zengchao@localhost ~]$ locate zengchao_test.file
[zengchao@localhost ~]$ sudo updatedb
[zengchao@localhost ~]$ locate zengchao_test.file
/home/zengchao/zengchao_test.file
[zengchao@localhost ~]$ rm -rf zengchao_test.file
[zengchao@localhost ~]$ locate zengchao_test.file
/home/zengchao/zengchao_test.file
[zengchao@localhost ~]$ sudo updatedb
[zengchao@localhost ~]$ locate zengchao_test.file
首先創(chuàng)建了一個文件,然后使用locate去查找,但是并沒有查找到,接著我手動更新數(shù)據(jù)庫文件,可以找到該文件.然后我刪除剛創(chuàng)建的文件,接著查找還是能找到,然后再次更新數(shù)據(jù)庫文件,這個時候就無法找到了.
which/whereis
which用于從系統(tǒng)的PATH變量所定義的目錄中查找可執(zhí)行的文件絕對路徑.例如超找passwd這個命令在系統(tǒng)中的絕對路徑.
[zengchao@localhost ~]$ which passwd
/bin/passwd
whereis查找也能查找到器路徑,但是與which不用的是,它不但能找出其二進制文件,還能找出相關(guān)的man文件.
zengchao@localhost ~]$ whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz