1. 程式人生 > >Linux上查找

Linux上查找

extra -- ... ## 大小寫 app pri color file

locate

  用法:locate filename

  locate是Linux系統中的一個查找(定位)文件命令,和find命令等找尋文件的工作原理類似,但locate是通過生成一個文件和文件夾的索引數據庫,當用戶在執行locate命令查找文件時,它會直接在索引數據庫裏查找,若該數據庫太久沒更新或不存在,在查找文件時就提示:

“locate: can not open `/var/lib/mlocate/mlocate.db‘: No such file or directory”。

此時執行“updatedb”

更新下數據庫即可。

[root@localhost keysystem]# updatedb
[root@localhost keysystem]# 

  用法示例:

[keysystem@localhost ~]$ locate a.txt 
/home/keysystem/a.txt
/usr/share/doc/sane-backends-1.0.21/matsushita/matsushita.txt
/usr/share/doc/vim-common-7.2.411/README_extra.txt
/usr/share/gnupg/help.ca.txt
/usr/share/gnupg/help.da.txt
/usr/share/gnupg/help.ja.txt
/usr/share/perl5/unicore/UnicodeData.txt
/usr/share/vim/vim72/doc/ft_ada.txt.gz
/usr/share/vim/vim72/doc/os_amiga.txt.gz /usr/share/vim/vim72/doc/uganda.txt.gz [keysystem@localhost ~]$

find

  find命令是一個無處不在命令,是Linux中最有用的命令之一。find命令用於:在一個目錄(及子目錄)中搜索文件,你可以指定一些匹配條件,如按文件名、文件類型、用戶甚至是時間戳查找文件。下面就通過實例來體驗下find命令的強大。

  用法:find

man文檔中給出的find命令的一般形式為:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

其實[-H] [-L] [-P] [-D debugopts] [-Olevel]這幾個選項並不常用(至少在我的日常工作中,沒有用到過),上面的find命令的常用形式可以簡化為:

find [path...] [expression]

  • path:find命令所查找的目錄路徑。例如用.來表示當前目錄,用/來表示系統根目錄
  • expression:expression可以分為——“-options [-print -exec -ok ...]”
  • -options,指定find命令的常用選項,下節詳細介紹
  • -print,find命令將匹配的文件輸出到標準輸出
  • -exec,find命令對匹配的文件執行該參數所給出的shell命令。相應命令的形式為‘command‘ { } \;,註意{ }和\;之間的空格

find ./ -size 0 -exec rm {} \; 刪除文件大小為零的文件 (還可以以這樣做:rm -i `find ./ -size 0` 或 find ./ -size 0 | xargs rm -f &)

find命令示例:

##查找當前目錄下類型是文件的
[keysystem@localhost redirect]$ find . -type f
./out.put
./file
./file1
./file2
./b.txt
./files.txt
./a.txt
[keysystem@localhost redirect]$ 
##查找當前目錄下類型是目錄的
[keysystem@localhost redirect]$ find . -type d . ./hello ./world
##查找當前目錄下類型是文件的  並用ls -l查看個文件   {}代表前面查找到的文件名
[keysystem@localhost redirect]$ find . -type f -exec ls -l ‘{}‘ ;
-rw-rw-r--. 1 keysystem keysystem 50 Dec  3 21:36 ./out.put
-rw-rw-r--. 1 keysystem keysystem 12 Dec  3 21:32 ./file
-rw-rw-r--. 1 keysystem keysystem 6 Dec  3 21:31 ./file1
-rw-rw-r--. 1 keysystem keysystem 6 Dec  3 21:31 ./file2
-rw-rw-r--. 1 keysystem keysystem 31 Dec  3 21:49 ./files.txt
##-printfind命令將匹配的文件輸出到標準輸出
[keysystem@localhost redirect]$ find . -type f -exec ls -l {} ; -print -rw-rw-r--. 1 keysystem keysystem 50 Dec 3 21:36 ./out.put ./out.put -rw-rw-r--. 1 keysystem keysystem 12 Dec 3 21:32 ./file ./file -rw-rw-r--. 1 keysystem keysystem 6 Dec 3 21:31 ./file1 ./file1 -rw-rw-r--. 1 keysystem keysystem 6 Dec 3 21:31 ./file2 ./file2 -rw-rw-r--. 1 keysystem keysystem 31 Dec 3 21:49 ./files.txt ./files.txt
##查找文件中包含hello的
[keysystem@localhost redirect]$ find . -type f -exec grep hello {} ; -print
hello
./file
hello
./file1
hello
./a.txt
##查找文件中包含Hello的 並打印出行號   -n 表示行號
[keysystem@localhost redirect]$ find . -type f -exec grep -n Hello {} ; -print
1:Hello
./b.txt
##查找文件中包含Hello的 並打印出行號   -n 表示行號 -i表示忽略大小寫
[keysystem@localhost redirect]$ find . -type f -exec grep -ni Hello {} ; -print
1:hello
./file
1:hello
./file1
1:Hello
./b.txt
1:hello
./a.txt

happygrep

Linux上查找