1. 程式人生 > >【每天一個Linux命令】15. 搜尋檔案命令find

【每天一個Linux命令】15. 搜尋檔案命令find

命令用途


find命令用於:在一個目錄(及子目錄)中搜索檔案,你可以指定一些匹配條件,如按檔名、檔案型別、使用者甚至是時間戳查詢檔案。

命令例項


0. 幫助命令:

[email protected]:~$ find -help
用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
預設路徑為當前目錄;預設表示式為 -print
表示式可能由下列成份組成:操作符、選項、測試表達式以及動作:
操作符 (優先順序遞減;未做任何指定時預設使用 -and):
( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2
EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2
位置選項 (總是真): -daystart -follow -regextype
普通選項 (總是真,在其它表示式前指定):
-depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
--version -xdev -ignore_readdir_race -noignore_readdir_race
測試(N可以是 +N 或-N 或 N):-amin N -anewer FILE -atime N -cmin 
-cnewer 檔案 -ctime N -empty -false -fstype 型別 -gid N -group 名稱
-ilname 匹配模式 -iname 匹配模式 -inum N -ipath 匹配模式 -iregex 匹配模式
-links N -lname 匹配模式 -mmin N -mtime N -name 匹配模式 -newer 檔案
-nouser -nogroup -path 匹配模式 -perm [+-]訪問模式 -regex 匹配模式
-readable -writable -executable
-wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
-used N -user NAME -xtype [bcdpfls]
動作: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print 
-fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
-exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
-execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;


1. 按照檔名查詢檔案 -name  

說明:
find /dir -name filename  在/dir目錄及其子目錄下面查詢名字為filename的檔案
find . -name "*.c" 在當前目錄及其子目錄(用“.”表示)中查詢任何副檔名為“c”的檔案
例子:
#查詢當前目錄下所有的sed.txt檔案
[email protected] ~$ find . -name sed.txt
./sed.txt

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

說明:
find . -perm 755 –print 在當前目錄下查詢檔案許可權位為755的檔案,即檔案屬主可以讀、寫、執行,其他使用者可以讀、執行的檔案

#例子:查詢當前目錄下檔案許可權為755的檔案
[email protected] testshell$ find . perm 755 -print
.
./dir1
./dir1/cpf1.txt
./dir1/cpf12.txt

3.不在當前指定的目錄中查詢 -prune

注意:如果同時使用-depth選項,那麼-prune將被find命令忽略
說明:
find /wirelessqa -path "/wirelessqa/bxp" -prune -o –print 在/apps目錄下查詢檔案,但不希望在/apps/bin目錄下查詢 
#例子:在當前目錄下查詢檔案,將dir3排除在查詢範圍之外
[email protected]
testshell$ ls -al total 0 drwxr-xr-x 8 bixiaopeng staff 272 Sep 18 17:00 . [email protected] 7 bixiaopeng staff 238 Sep 18 15:43 .. drwxr-xr-x 7 bixiaopeng staff 238 Sep 18 17:27 dir1 drwxr-xr-x 5 bixiaopeng staff 170 Sep 18 16:50 dir11 drwxr-xr-x 7 bixiaopeng staff 238 Sep 18 17:31 dir2 drwxr-xr-x 5 bixiaopeng staff 170 Sep 18 16:53 dir3 -rw-r--r-- 1 bixiaopeng staff 0 Sep 18 16:15 f2.txt -rw-r--r-- 1 bixiaopeng staff 0 Sep 18 17:00 f3.txt [email protected] testshell$ find . -path "./dir3" -prune -o -print . ./dir1 ./dir1/cpf1.txt ./dir1/cpf12.txt ./dir1/ddddir ./dir1/f1.txt ./dir1/f2.txt ./dir11 ./dir11/cpf1.txt ./dir11/cpf12.txt ./dir11/f1.txt ./dir2 ./dir2/cpf1.txt ./dir2/cpf12.txt ./dir2/ddddir ./dir2/f1.txt ./dir2/f2.txt ./f2.txt ./f3.txt

4. 按照檔案屬主來查詢檔案 -user

find ~ -user bixiaopeng –print 在$HOME目錄中查詢檔案屬主為bixiaopeng的檔案
 例子:#在$HOME目錄中查詢檔案屬主為bixiaopeng的檔案
[email protected] testshell$ find ~ -user bixiaopeng -print 
/Users/bixiaopeng/source-code/workshop/Wireless-Test-Workshop/wtc/web/report/src/main/webapp/WEB-INF/web.xml
/Users/bixiaopeng/testfun1.sh
/Users/bixiaopeng/testfun2.sh
/Users/bixiaopeng/testfunc.sh
/Users/bixiaopeng/testgetopts.sh
…...

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

說明:
find /apps -group gem –print 在/apps目錄下查詢屬於gem使用者組的檔案 
#例子:在當前目錄下查詢屬於staff使用者組的檔案
[email protected] testshell$ find . -group staff -print
.
./dir1
./dir1/cpf1.txt
./dir1/cpf12.txt
./dir1/ddddir
./dir1/f1.txt
….

6. 按照檔案的更改時間來查詢檔案 -mtime -n +n

注意:- n表示檔案更改時間距現在n天以內,+ n表示檔案更改時間距現在n天以前
說明:
find / -mtime -5 –print 在系統根目錄下查詢更改時間在5日以內的檔案 
find /var/adm -mtime +3 –print 在/var/adm目錄下查詢更改時間在3日以前的檔案
#例子:查詢當前目錄下更改時間在1日以內的檔案
[email protected] testshell$ find . -mtime -1 -print
.
./dir1
./dir1/cpf1.txt
./dir1/cpf12.txt
./dir1/ddddir
./dir1/f1.txt
./dir1/f2.txt
./dir11
...

7. 查詢無有效所屬組的檔案

即該檔案所屬的組在/etc/groups中不存在 -nogroup 
說明:find / –nogroup -print

8. 查詢無有效屬主的檔案

即該檔案的屬主在/etc/passwd中不存在 -nouser 
說明:find /home -nouser –print

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

 說明: find -newer file1 !file2

10. 查詢某一型別的檔案 -type 

說明:
型別檔案: 
b - 塊裝置檔案。 
d - 目錄。 
c - 字元裝置檔案。 
p - 管道檔案。 
l - 符號連結檔案。 
f - 普通檔案。 

find /etc -type d –print 在/etc目錄下查詢所有的目錄 
find . ! -type d –print 在當前目錄下查詢除目錄以外的所有型別的檔案 
find /etc -type l –print 在/etc目錄下查詢所有的符號連結檔案

#例子:在當前目錄下查詢所有的目錄
[email protected] testshell$ find . -type d -print
.
./dir1
./dir1/ddddir
./dir11
./dir2
./dir2/ddddir
./dir3
./dir3/dir1
 #例子:在當前目錄下查詢除目錄以外的所有檔案
[email protected] testshell$ find . ! -type d -print
./dir1/cpf1.txt
./dir1/cpf12.txt
./dir1/f1.txt
./dir1/f2.txt
./dir11/cpf1.txt
./dir11/cpf12.txt
./dir11/f1.txt
./dir2/cpf1.txt
./dir2/cpf12.txt
./dir2/f1.txt
./dir2/f2.txt
./dir3/cpf1.txt
./dir3/cpf12.txt
./dir3/dir1/cpf1.txt
./dir3/dir1/cpf12.txt
./dir3/dir1/f1.txt
./f2.txt
./f3.txt
#例子:在當前目錄下查詢其它檔案型別
[email protected] testshell$ find . -type l -print
[email protected] testshell$ find . -type c -print
[email protected] testshell$ find . -type p -print
[email protected] testshell$ find . -type f -print
./dir1/cpf1.txt
./dir1/cpf12.txt
./dir1/f1.txt
./dir1/f2.txt
./dir11/cpf1.txt
./dir11/cpf12.txt
./dir11/f1.txt
./dir2/cpf1.txt
./dir2/cpf12.txt
./dir2/f1.txt
./dir2/f2.txt
./dir3/cpf1.txt
./dir3/cpf12.txt
./dir3/dir1/cpf1.txt
./dir3/dir1/cpf12.txt
./dir3/dir1/f1.txt
./f2.txt
./f3.txt

11. 以檔案大小來查詢 -size n

 -size n:[c] 查詢檔案長度為n塊的檔案,帶有c時表示檔案長度以位元組計
說明:
find . -size +1000000c –print 在當前目錄下查詢檔案長度大於1 M位元組的檔案 
find . -size 100c –print 在當前目錄下查詢檔案長度恰好為100位元組的檔案 
find . -size +10 –print 在當前目錄下查詢長度超過10塊的檔案(一塊等於512位元組)
 #例子:查詢當前目錄下為300位元組的檔案
[email protected] Projects$ find . size 300c -print 
./workspaces/嬋???????/.settings
./workspaces/嬋???????/.settings/org.eclipse.wst.server.core.prefs
./workspaces/嬋???????/Tomcat v7.0 Server @ localhost-config
./workspaces/嬋???????/Tomcat v7.0 Server @ localhost-config/catalina.policy
./workspaces/嬋???????/Tomcat v7.0 Server @ localhost-config/catalina.properties
./workspaces/嬋???????/Tomcat v7.0 Server @ localhost-config/context.xml
./workspaces/嬋???????/Tomcat v7.0 Server @ localhost-config/server.xml
….

12. 查詢檔案時首先從當前目錄查詢,然後再查詢子目錄

-depth:在查詢檔案時,首先查詢當前目錄中的檔案,然後再在其子目錄中查詢
說明:
find / -name "bixiaopeng" -depth –print 它將首先匹配所有的檔案然後再進入子目錄中查詢 

13. 在查詢檔案時不跨越檔案系統mount點 -mount

說明:
find . -name "*.sh" -mount –print 從當前目錄開始查詢位於本檔案系統中檔名以sh結尾的檔案(不進入其他檔案系統)
 #例子:在當前目錄查詢位於本檔案系統以.zip結尾的檔案
[email protected] Projects$ find . -name "*.zip" -mount -print
./workspaces/.metadata/.mylyn/.tasks.xml.zip
./workspaces/.metadata/.mylyn/repositories.xml.zip
./workspaces/.metadata/.mylyn/tasks.xml.zip
./workspaces/Android Context璇﹁В蹇?????????????.zip
./workspaces/WirelessQa-EditText.zip
./workspaces/WirelessQA-Location.zip
./workspaces/WirelessQA-Provider.zip

14. 如果find命令遇到符號連結檔案,就跟蹤至連結所指向的檔案 -follow


15.在當前目錄及所有子目錄中查詢filename(忽略大小寫)

# find -iname "filename"

16.在根目錄分級查詢

查詢根目錄和根目錄的和只展開一級的子目錄中查詢
$find -maxdepth 2 -name passwd
在根目錄和根目錄下展開兩級查詢passwd檔案
$find / -maxdepth 3 -name passwd
在根目錄的第二級和第四級之間查詢
$ find -mindepth 3 -maxdepth 5 -name passwd

find與exec和ok一起使用

當匹配到一些檔案以後,可能希望對其進行某些操作,這時就可以使用-exec選項,一旦find命令匹配到了相應的檔案,就可以用-exec選項中的命令對其進行操作.
格式:   -exec command {} \;
exec選項後面跟隨著所要執行的命令,然後是一對兒{},一個空格和一個\,最後是一個分號

#例子:在當前目錄下查詢檔案,並使用exec執行ls -l命令
[email protected] androidshell$ find .  -type f -exec ls -l {} \;
-rw-r--r--  1 bixiaopeng  staff  24576 Jul  3 10:03 ./.1.log.swp
[email protected] 1 bixiaopeng  staff  6148 Sep 18 15:43 ./.DS_Store
-rw-r--r--  1 bixiaopeng  staff  683295 Jun  5 10:39 ./1.log
-rw-r--r--  1 bixiaopeng  staff  171 Jun  5 10:32 ./logcat.sh
-rw-r--r--  1 bixiaopeng  staff  120 Jun  4 17:34 ./monkey.sh

#例子:查詢並刪除dir1目錄下更改時間小於一天的檔案
[email protected] testshell$ find dir1 -type f -mtime -1 -exec rm {} \;
[email protected] testshell$ ls dir1 -al
ls: -al: No such file or directory
dir1:
ddddir
[email protected] testshell$ ls -al dir1
total 0
drwxr-xr-x  3 bixiaopeng  staff  102 Sep 19 13:30 .
drwxr-xr-x  8 bixiaopeng  staff  272 Sep 18 17:00 ..
drwxr-xr-x  2 bixiaopeng  staff   68 Sep 18 17:27 ddddir
#例子:查詢並刪除dir1目錄下更改時間小於一天的檔案,刪除時提示是否刪除  y 刪除  n不刪除
# 使用-ok
[email protected] testshell$ find dir2 -name "*.txt" -mtime -1 -ok rm {} \;
"rm dir2/cpf1.txt"? y
"rm dir2/cpf12.txt"? y
"rm dir2/f1.txt"? y
"rm dir2/f2.txt"? n

#使用-exec
[email protected] testshell$ find dir2 -name "*.txt" -mtime -1 -exec rm -i {} \;
remove dir2/f2.txt? n

find與xargs一起使用

find命令把匹配到的檔案傳遞給xargs命令,而xargs命令每次只獲取一部分檔案而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分檔案,然後是下一批,並如此繼續下去。

#查詢系統中的每一個普通檔案,然後使用xargs命令來測試它們分別屬於哪類檔案:find . -type f -print | xargs file 
[email protected] androidshell$ find . -type f -print |xargs file
./.1.log.swp: Vim swap file, version 7.3
./.DS_Store:  data
./1.log:      UTF-8 Unicode text, with very long lines, with CRLF, CR line terminators
./logcat.sh:  Bourne-Again shell script text executable
./monkey.sh:  Bourne-Again shell script text executable

#在整個系統中查詢記憶體資訊轉儲檔案(core dump) ,然後把結果儲存到/tmp/core.log 檔案中:
find / -name "core" -print | xargs echo "" >/tmp/core.log 

#用grep命令在所有的普通檔案中搜索wirelessqa這個詞
find . -type f -print | xargs grep "wirelessqa" 
#在/bixiaopeng/apps目錄下查詢所有使用者具有讀、寫和執行許可權的檔案,並收回相應的寫許可權:
$ find /bixiaopeng/apps  -perm -7 -print | xargs chmod o -w
#在當前目錄下的所有普通檔案中搜索ILOVEU這個詞
$ find . -name *\ -type f -print  | xargs grep "ILOVEU"
#刪除3天以前的所有東西 (find . -ctime +3 -exec rm -rf {} \;)
find ./ -mtime +3 -print|xargs rm -f –r 
#刪除檔案大小為零的檔案
find . -size 0 | xargs rm -f & 

本文參考眾多網上資料,感謝樂於分享的人們。



相關推薦

每天一個Linux命令15. 搜尋檔案命令find

命令用途 find命令用於:在一個目錄(及子目錄)中搜索檔案,你可以指定一些匹配條件,如按檔名、檔案型別、使用者甚至是時間戳查詢檔案。命令例項 0. 幫助命令: [email protected]:~$ find -help 用法: find [-H] [-L]

每天一個linux命令wc

ror class pre inux 單詞 字節數 出現 行數 同一行 wc:統計 # wc -l/c/w #統計行數/字節數/字數(1個單詞/1個中文算一個數) wc -l yy.log cat yy.log |wc -l 1、當前目錄下文件個數 ls -lR

每天一個linux命令curl

bsp linux cit 焦作 pos 參數 match 轉義 url curl:訪問http請求 1、不帶參數的 get 訪問網址 curl https://www.baidu.com/ 2、帶參數的 post 訪問網址 # 原始http請求1 http://19

每天一個linux命令grep

http pre 內容 技術分享 image 忽略 linux filename com 【簡介】 grep:一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來 【grep常用用法】 [root@www ~]# grep [-acinv] [--col

每天一個Linux命令25. 檢視檔案命令(cat/more/less/tail/tac/nl/od)

linux中檢視檔案的命令如下: cat: 由第一行開始顯示檔案內容 tac: 從最後一行開始顯示,可以看出 tac 是 cat 的反向顯示! nl: 顯示的時候,隨便輸出行號! more: 一頁一頁的顯示檔案內容less 與 more 類似,但是比 more 更好的是,他

每天一個Linux命令01. Linux中ping命令的用法

PING: PING (Packet Internet Grope),因特網包探索器 功能說明: 檢測網路的連通性:ping可以測試計算機名和計算機的 ip 地址,驗證與遠端計算機的連線,通過將 icmp 回顯資料包傳送到計算機並偵聽回顯回覆資料包來驗證與一臺

每天一個Linux命令27. 建立連結命令ln

什麼是連結(link)?Linux檔案系統中,有所謂的連結(link),我們可以將其視為檔案的別名連結又可分為兩種 : 硬連結(hard link)與軟連結(symbolic link)硬連結是存在同一個檔案系統中,而軟連結卻可以跨越不同的檔案系統。什麼是硬連結?硬連結的意

每天一個linux命令11. Linux配置環境變數/etc/profile .bashrc .bash_profile等

1. 修改/etc/profile檔案 特點:所有使用者的shell都有權使用你配置好的環境變數說明:如果你的電腦僅用作開發,建議使用此配置,因為所有使用者的shell都有權使用你配置好的環境變數,

每天一個linux命令01.ls命令

每天一個linux命令(1):ls命令 ls命令是linux下最常用的命令。ls命令就是list的縮寫預設下ls用來打印出當前目錄的清單如果ls指定其他目錄那麼就會顯示指定目錄裡的檔案及資料夾清單。 通過ls 命令不僅可以檢視linux資料夾包含的檔案

每天一個Linux命令B-badblocks

badblocks命令用於檢查磁碟裝置中損壞的區塊。 執行指令時須指定所要檢查的磁碟裝置,及此裝置的磁碟區塊數。 語法 badblocks [-svw][-b <區塊大小>][-o <輸出檔案>][磁碟裝置][磁碟區塊數][啟始區塊

每天一個Linux命令08. Linux中time命令的用法

Linux中time命令,我們經常用來計算某個程式的執行耗時(real),使用者態cpu耗時(user),系統態cpu耗時(sys) time命令最常用的使用方式就是在其後面直接跟上命令和引數:ti

轉載每天一個Linux命令

mkdir 命令 結構 oca archive mic post rmdir 命令 每天 sof 每天一個linux命令(1) : ls 命令 每天一個linux命令(2) : cd 命令 每天一個linux命令(3) : pwd 命令 每天一個linux命令(4)

每天一個linux命令(文件操作):轉載find命令之xargs

溢出 系統性能 問題 ameba pri 幾分鐘 body 只有一個 獲取參數   在使用 find命令的-exec選項處理匹配到的文件時, find命令將所有匹配到的文件一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令運行幾分鐘之

每天一個linux命令(文件上傳下載文件操作):轉載tar命令

設備 mil second compress bsp 新的 body 創建 命令格式   通過SSH訪問服務器,難免會要用到壓縮,解壓縮,打包,解包等,這時候tar命令就是是必不可少的一個功能強大的工具。linux中最流行的tar是麻雀雖小,五臟俱全,功能強大。   tar

每天一個linux命令(文件上傳下載文件操作):轉載gzip命令

明顯 相關信息 users 版本信息 通過 inux 備份 示例 strong   減少文件大小有兩個明顯的好處,一是可以減少存儲空間,二是通過網絡傳輸文件時,可以減少傳輸的時間。gzip是在Linux系統中經常使用的一個對文件進行壓縮和解壓縮的命令,既方便又好用。gzip

每天一個linux命令(44):top命令

top命令是Linux下常用的效能分析工具,能夠實時顯示系統中各個程序的資源佔用狀況,類似於Windows的工作管理員。下面詳細介紹它的使用方法。top是一個動態顯示過程,即可以通過使用者按鍵來不斷重新整理當前狀態.如果在前臺執行該命令,它將獨佔前臺,直到使用者終止

每天一個linux命令(20):常用檔案搜尋命令locate/find/which/whereis

004.檔案搜尋命令         04-01.檔案搜尋命令locate             a.locate 功能單一,只搜尋檔名; &nbs

每天一個linux命令15):tail 命令,實時列印TOMCAT日誌

tail 命令從指定點開始將檔案寫到標準輸出.使用tail命令的-f選項可以方便的查閱正在改變的日誌檔案,tail -f filename會把filename裡最尾部的內容顯示在螢幕上,並且不但重新整理,使你看到最新的檔案內容.  1.命令格式; tail[必要引數][選擇引數][檔案]    2.命令功能

每天一個linux命令(15):tail命令

1、命令簡介 tail (tail) 用來顯示檔案的結尾(預設為10行)至標準輸出中。若指定了多於一個檔案,程式會在每段輸出的開始新增相應檔名作為頭。如果不指定檔案或檔案為"-" ,則從標準輸入讀取資料。 2、用法 nl [選項]... [檔案]... 3、選項 -c, --bytes=

每天一個Linux命令(15):tail

從指定點開始,tail將檔案寫到標準輸出。使用-f選項,我們可以方便地查閱正在改變的日誌檔案,tail -f filename會把filename檔案中最尾部的內容顯示在螢幕上,並且不斷地重新整理,使我們看到檔案最新的內容。 1. 命令格式 tail