Linux中查詢檔案的常見命令

2021-07-04 08:18:36 字數 3394 閱讀 2538

linux下find命令在目錄結構中搜尋檔案,並執行指定的操作。linux下find命令提供了相當多的查詢條件,功能很強大。

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

find [-h] [-l] [-p] [-d debugopts] [-olevel] [path...] [expression]

說明:

default path is the current directory; default expression is -print

expression may consist of: operators, options, tests, and actions:

其實[-h] [-l] [-p] [-d debugopts] [-olevel]這幾個選項並不常用(才疏學淺),上面的find命令的常用形式可以簡化為:

find [path...] [expression]

更加一般的格式為:

find  [

path...

]  [

options

]  [action]

path:find命令所查詢的目錄路徑。

expression:expression可以分為——「-options [-print -exec -ok ...]」 

-options,指定find命令的常用選項

-print,find命令將匹配的檔案輸出到標準輸出

-exec,find命令對匹配的檔案執行該引數所給出的shell命令。相應命令的形式為'command'  '{}' \;,注意和\;之間的空格

options常用選項

-name 按照檔名查詢檔案

-perm  按照檔案許可權來查詢檔案

-user

按照檔案屬主來查詢檔案

-group 按照檔案所屬的組來查詢檔案

-mtime -n +n          查詢n天內/n天前更改過的檔案

-atime -n +n           查詢n天內/n天前訪問過的檔案

-ctime -n +n            查詢n天內/n天前建立的檔案

-size 根據檔案大小查詢

-type  b/d/c/p/l/f/s

查詢某一型別的檔案 

-newer file1 ! file2  查詢更改時間比檔案file1新但比檔案file2舊的檔案

action選項

-print                                 //預設情況下的動作

-ls                                     //查詢到後用ls 顯示出來

-ok  [commend]              //查詢後執行命令的時候詢問使用者是否要執行

-exec [commend]          //查詢後執行命令的時候不詢問使用者,直接執行

刪除檔案大小為零的檔案:

find ./ -size 0 -exec rm '{}' \;  或者rm -i `find ./ -size 0`   或  find ./ -size 0 | xargs rm -f &

在$home中查.txt檔案並顯示:

find   ~   -name   "*.txt"   -print   

查詢大於1,500位元組大小的檔案:

find / -size +1500c

檢視當前目錄下以.text為結尾的普通檔案

find . -type f -name *.text -exec ls -l '{}' \;

locate 讓使用者可以很快速的搜尋檔案系統內是否有指定的檔案。其方法是先建立乙個包括系統內所有檔名稱及路徑的資料庫,之後當尋找時就只需查詢這個資料庫,而不必實際深入檔案系統之中了。在一般的 distribution 之中,資料庫的建立都被放在 crontab 中自動執行。

locate命令可以在搜尋資料庫時快速找到檔案,資料庫由updatedb程式來更新,updatedb是由cron daemon週期性建立的,locate命令在搜尋資料庫時比由整個由硬碟資料來搜尋資料來得快,但較差勁的是locate所找到的檔案若是最近才建立或 剛更名的,可能會找不到,在內定值中,updatedb每天會跑一次,可以由修改crontab來更新設定值。(etc/crontab)

locate [option]... pattern...

[root@lsl locatedeme]# locate -v

mlocate 0.22.2

this software is distributed under the gpl v.2.

this program is provided with no warranty, to the extent permitted by law.

[root@lsl locatedeme]# touch locate_test

[root@lsl locatedeme]# find ./ -name locate_test

./locate_test

[root@lsl locatedeme]# locate locate_test

[root@lsl locatedeme]# updatedb

[root@lsl locatedeme]# locate locate_test

/root/study/locatedeme/locate_test

[root@lsl locatedeme]# rm locate_test

rm: remove regular empty file `locate_test'? y

[root@lsl locatedeme]# locate locate_test

/root/study/locatedeme/locate_test

[root@lsl locatedeme]# updatedb

[root@lsl locatedeme]# locate locate_test

[root@lsl locatedeme]#

which用於人系統的path變數所定義的目錄中查詢可執行檔案的絕對路徑和相關命令的別名。

比如查詢ls命令

[root@lsl locatedeme]# which ls

alias ls='ls --color=auto'

/bin/ls

使用whereis也能查到路徑,但是和which不同的是,它不但能找出其二進位制檔案,還能找出相關的man檔案。

比如

[root@lsl locatedeme]# whereis ls

ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

linux中查詢檔案命令

linux下檔案搜尋 查詢 檢視命令 1 最強大的搜尋命令 find 查詢各種檔案的命令 2 在檔案資料中查詢檔案 locate 3 搜尋命令所在的目錄及別名資訊 which 4 搜尋命令所在的目錄及幫助文件路徑 whereis5 在檔案中搜尋字串匹配的行並輸出 grep6 分頁顯示乙個檔案或任何輸...

linux查詢檔案命令

在使用linux時,經常需要進行檔案查詢。其中查詢的命令主要有find和grep。兩個命令是有區的。區別 1 find命令是根據檔案的屬性進行查詢,如檔名,檔案大小,所有者,所屬組,是否為空,訪問時間,修改時間等。2 grep是根據檔案的內容進行查詢,會對檔案的每一行按照給定的模式 patter 進...

linux 查詢檔案命令

linux下檔案搜尋 查詢 檢視命令 1 最強大的搜尋命令 find 查詢各種檔案的命令 2 在檔案資料中查詢檔案 locate 3 搜尋命令所在的目錄及別名資訊 which 4 搜尋命令所在的目錄及幫助文件路徑 whereis5 在檔案中搜尋字串匹配的行並輸出 grep6 分頁顯示乙個檔案或任何輸...