find命令
find命令是linux下面一個常用的命令,說它簡單可以,說它複雜也可以。它有太多的引數,要完全掌握不容易,下面我就做個翻譯,並附上範例,寫寫常用的引數。
一、基礎
首先要知道的是,find相當耗io資源,如果不是必要的話,建議用locate代替。
find的命令格式如下:
[code]
find pathname -options
pathname 是要搜尋的路徑
-options 是可以增加的選項
[/code]
選項包括:
[quote]-name
按照檔名查詢檔案,支援*號和[]號。
-perm
按照檔案許可權來查詢檔案,支援完全指定和-號、+號部分符合。
-prune
使用這一選項可以使find命令不在當前指定的目錄中查詢,如果同時使用-depth選項,那麼-prune將被find命令忽略。
-user
按照檔案屬主來查詢檔案。
-group
按照檔案所屬的組來查詢檔案。
-mtime -n +n -1當天
按照檔案的更改時間來查詢檔案
-amin -n +n
按照檔案的訪問時間來查詢檔案
-cmin -n +n
按照檔案狀態的更改時間來查詢檔案
※- n表示檔案更改時間距現在n天以內,+ n表示檔案更改時間距現在n天以前。
-nogroup
查詢無有效所屬組的檔案,即該檔案所屬的組在/etc/groups中不存在。
-nouser
查詢無有效屬主的檔案,即該檔案的屬主在/etc/passwd中不存在。
-newer file1 ! -newer file2
查詢更改時間比檔案file1新但比檔案file2舊的檔案。
-type
查詢某一型別的檔案,諸如:b - 塊裝置檔案。d - 目錄。c - 字元裝置檔案。p - 管道檔案。l - 符號連結檔案。f - 普通檔案。
-size n:[c] 查詢檔案長度為n塊的檔案,帶有c時表示檔案長度以位元組計。
-depth:在查詢檔案時,首先查詢當前目錄中的檔案,然後再在其子目錄中查詢。
-fstype:查詢位於某一型別檔案系統中的檔案,這些檔案系統型別通常可以在配置檔案/etc/fstab中找到,該配置檔案中包含了本系統中有關檔案系統的資訊。
-mount:在查詢檔案時不跨越檔案系統mount點。
-follow:如果find命令遇到符號連結檔案,就跟蹤至連結所指向的檔案。
-regex pattern:對搜尋結果的[color=#DC143C]整個路徑[/color]按正規表示式進行過濾,必須對全路徑考慮,例如結果是./test,那正規表示式應該用"./t.*",而不能用"t.*"
-cpio:對匹配的檔案使用cpio命令。[/quote]
另外,在使用多個引數的時候,find命令還支援使用邏輯運算子:
[quote]-a 類似&&,也就是與,當第一個引數執行為真,才執行後面的引數
-o 類似||,也就是或,當第一個執行為假,則執行後面的引數
!,也就是非、否[/quote]
二、樣例
[code]$ find ~ -name "*.txt" -print[/code]
查詢使用者主目錄下所有結尾是.txt的檔案和目錄等
[code]$ find . -type f -perm 644 -print[/code]
顯示當前目錄下的所有許可權是644的普通檔案
[code]$ find /var/logs -type f -mtime +7[/code]
顯示/var/logs目錄下,更改時間在7日前的檔案
[code]$ find . -perm -7 -print[/code]
顯示當前目錄下許可權為777的檔案和目錄等,-7表示使用者、主、其他許可權中都必須是7。
[quote]-號表示許可權要全包含,例如-4,則644、646、446都可以找到,但640就不會顯示;
+號表示許可權可以部分包含,例如+4,則644、644甚至640、404都會顯示;[/quote]
[code]$ find /apps -path "/apps/bin" -prune -o -print[/code]
顯示/apps目錄中,除/apps/bin目錄的所有檔案和目錄等;也就是當-path "/apps/bin"找到,則-prune,否則就-print,類似於:
[quote]if -path "/usr/sam" then
-prune
else
-print[/quote]
[code]find /test \(-path /test/dir1 -o -path /test/file1 \) -prune -o -name "temp" -print[/code]
找/test目錄下除了/test/dir1和/test/file1目錄外的其他目錄中的temp檔案和目錄等
[code]$ find . -newer httpd1.conf ! -newer temp [/code]
找當前目錄下比httpd1.conf新,但比temp舊的檔案和目錄等
[code]$ find . ! -type d -print[/code]
找當前目錄下除目錄外的其他檔案等(包括裝置、連結檔案)
[code]$ find . -size +1000000c -print[/code]
顯示當前目錄檔案大小超過1M位元組的檔案
[code]$ find /home/linuxing -size 100c -print[/code]
找/home/linuxing目錄下剛好100位元組的檔案
[code]$ find . -size +10 -print[/code]
找當前目錄下超過10個塊的檔案(1個塊預設是512位元組)
[code]$ find / -name "test" -depth -print[/code]
按先找完起始目錄,再進入子目錄查詢的方式找test檔案
[code]$ find . -name "test" -mount -print[/code]
找位於本檔案系統上的當前目錄中的test檔案或目錄等(不跨檔案系統)
[code]
$ ls -l
total 2
-rw-r--r-- 1 root wheel 0 Dec 28 11:39 001
-rw-r--r-- 1 root wheel 0 Dec 28 11:39 002
-rw-r--r-- 1 root wheel 36 Dec 28 18:05 test
$ find . -name "00*" -regex "00.*"
$ find . -name "00*" -regex "\./00.*"
./001
./002
$ find . -name "00*" -regex ".*0."
./001
./002
[/code]
-regex 後面跟的是正規表示式,並且是對包括全路徑的結果做過濾
三、配合引數-exec、-ok或xargs
find命令可以使用-exec或-ok引數直接對結果執行操作,-exec或-ok後面跟執行的命令,然後是一對兒{},一個空格和一個\,最後是一個分號。
-exec和-ok的區別是-exec是不會諮詢使用者是否確認的,但-ok會要使用者對操作進行每一步的確認。
另外,由於部分系統能否接受的引數是有限制的,但-exec和-ok是把所有的結果作為整套引數傳遞,這會引起問題。所以也可以使用xargs配合達到同樣的目的。在使用xargs命令時,究竟是一次獲取所有的引數,還是分批取得引數,以及每一次獲取引數的數目都會根據該命令的選項及系統核心中相應的可調引數來確定。
[code]# find . -name "00*" -exec ls -l {} \;
-rw-r--r-- 1 root wheel 0 Dec 28 11:39 ./001
-rw-r--r-- 1 root wheel 0 Dec 28 11:39 ./002[/code]
對比上面命令的結果
[code]# find . -name "00*" -ok rm {} \;
"rm ./001"? n
"rm ./002"? n
# find /etc -name "passwd*" -exec grep "linuxing" {} \;
linuxing:*:1010:1010:linuxing:/home/linuxing:/bin/bash[/code]
對比-exec和-ok的結果
# find . -type f -name "00*" |xargs file
./001: ASCII text
./002: empty
[code]# find . -perm 666 -print | xargs chmod o-w[/code]
再找到本地目錄中有666許可權的檔案或目錄等後,把other的w許可權去掉
[code]# find /etc -name "passwd*" |xargs grep "linuxing"
/etc/passwd:linuxing:*:1010:1010:linuxing:/home/linuxing:/bin/bash[/code]
對比xargs和-exec、-ok的使用方式和結果
四、注意
1、find的後面跟的引數需要注意順序
因為find的引數之間可以省略-a,也就是說
find . -name "test*" -type f
和
find . -name "test*" -a -type f
是一樣的。一旦配合-o等判斷的時候,可能會引起難以發現的誤會。
例如下面的命令:
[code]find /test \(-path /test/dir1 -o -path /test/file1 \) -prune -o -name "temp" -print[/code]
2、find引數不只上述的這些,更多的請man看看。使用好find命令可以簡單很多的操作,但同時find也是相當耗費資源的命令,需要慎用。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27038344/viewspace-732076/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- find 命令
- find -newer命令
- find 命令入門
- grep、find命令整理
- Find命令總彙
- find命令詳解
- find 命令專題
- find 命令詳解
- linux find命令Linux
- linux find 命令Linux
- Find命令練習
- linux find 命令!Linux
- find 命令全面解析
- [shell基礎]——find命令
- find命令檔名字尾
- 我常用的find命令
- linux/unix find命令Linux
- linux中file命令和find命令Linux
- Linux find 命令詳解Linux
- Linux find命令詳解Linux
- [轉]UNIX find命令實踐
- Linux精講——find命令Linux
- aix基本命令之findAI
- linux每日命令(20):find命令概覽Linux
- AIX中find命令和xargs命令介紹AI
- sed命令和find命令的結合的使用
- find 命令的引數詳解
- Linux 常用基本命令 findLinux
- (轉)Linux find命令詳解Linux
- find命令-mtime引數理解
- Linux下find命令詳解Linux
- find 的命令的使用方法。
- 常用find命令與xargs.txt
- Linux Find 命令精通指南Linux
- 每天一個linux命令:find命令之xargsLinux
- linux find 命令和 while 命令聯合使用案例LinuxWhile
- Linux/Unix重要find命令詳解Linux
- 在 Linux中find命令使用技巧Linux