Linux命令之find(二),linux命令find
上篇文章已經介紹了expressions的的test,它是find命令最核心的東西,現在介紹expressions的options和actions。
(一)options
m@meng:~/patches/tmp$ find . -maxdepth 1 -name 'onlyme*' ./onlyme ./onlyme1 m@meng:~/patches/tmp$ find . -maxdepth 2 -name 'onlyme*' find . -maxdepth 2 -name 'onlyme*' ./test/onlyme5 ./test/onlyme.sh ./onlyme ./onlyme1
- -mindepth:類似上面的-maxdepth,但是指定的是最小深度。比如指
定 2,那就是只搜尋深度大於2的各級子目錄,而不理會小於2的那些目錄(小於2的就是指定目錄本身),如下例:
m@meng:~/patches/tmp$ find . -mindepth 1 -name 'onlyme*' ./test/onlyme5 ./test/onlyme.sh ./onlyme ./onlyme1 m@meng:~/patches/tmp$ find . -mindepth 2 -name 'onlyme*' ./test/onlyme5 ./test/onlyme.sh
- -help:顯示協助資訊,並退出。
- -mount或-xdev:如果某個(子)目錄是另一個檔案系統的掛載點,則跳過該目錄。
-daystart:先看man手冊的說法:Measure times (for -amin, -atime, -cmin, -ctime, -mmin, and -mtime) from the beginning of today rather than from24 hours ago. 看來這個選項隻影響那些跟時間有關的test,而且改變了時間的計算方式,即不再從當前時刻開始計算,而是從每天的0:00開始計算。前面說過,-atime n,這個n並不代表實際的“一天”,而是指24小時;但是加上-daystart這個選項之後,就可以真的代表“一天”了,因為這時就是從每天的0:00開始計時的,看下面的例子:
m@meng:~/patches/tmp$ date2015年 07月 12日 星期日 00:13:47 CSTm@meng:~/patches/tmp$ stat new最近訪問:2015-07-12 00:10:15.707908310 +0800最近更改:2015-07-12 00:10:15.707908310 +0800最近改動:2015-07-12 00:10:15.707908310 +0800m@meng:~/patches/tmp$ stat test最近訪問:2015-07-11 23:42:25.064527595 +0800最近更改:2015-07-06 23:57:45.095385673 +0800最近改動:2015-07-06 23:57:45.095385673 +0800m@meng:~/patches/tmp$ find -atime 0../test./newm@meng:~/patches/tmp$ find -daystart -atime 0../new
這就很顯然了,在沒有加上-daystart這個選項之前,-atime 0指的是從當前這一刻開始,往前的24小時之內,如果某個檔案被訪問過,則匹配;但是加上-daystart之後,-atime 0表示今天被訪問過的檔案才會匹配。同理-atime 1表示昨天(即7/11 0:00 ~ 7/11 23:59)被訪問過的檔案才會匹配。
這個選項的作用可以簡單歸納為:化零為整。
(二)actions
- -delete:找到匹配的檔案之後,將其刪除。man手冊上提到:Use of -delete automatically turns on the -depth option,不過我還沒搞懂這個-depth到底有什麼卵用。。
- -print:這個是預設的action,即沒有指定任何action的時候其實就是使用了這個-print,但是指定了另一個action的時候,就必須顯式加上-print才有效果。它把每個匹配的檔案列印到標準輸出上,每行一個檔案。這意味著,-print在列印的時候,每個檔案末尾添加了一個分行符號。
但是這個分行符號是為了瀏覽方便,有時會造成一些麻煩,比如使用管道的時候;如果想省略這個分行符號,可以用-print0來代替,如下:
m@meng:~/patches/tmp$ find -daystart -atime 0 -print . ./new m@meng:~/patches/tmp$ find -daystart -atime 0 -print0 ../newm@meng:~/patches/tmp$
所以,如果只是想在終端觀察輸出,沒有必要使用-print0,-print就足夠了。
-print還有一個變種是-printf,類似於C語言中的printf函數,進行格式化輸出,暫時不研究了,有太多格式。
- -ls:類似於ls命令的-dils選項組合,如下:
m@meng:~/patches/tmp$ find -daystart -atime 0 -ls 7478778 4 drwxrwxr-x 3 m m 4096 7月 12 00:10 . 7478967 0 -rw-rw-r-- 1 m m 0 7月 12 00:10 ./new m@meng:~/patches/tmp$ ls -dils new 7478967 0 -rw-rw-r-- 1 m m 0 7月 12 00:10 new
換了一種更詳細的輸出而已。
f系列,即-(fls/fprint/fprint0/fprintf) file,這幾個action與沒有開頭f的那幾個action是非常相似的,只是不再把結果列印到標準輸出,而是輸出到檔案file中。
-exec command ;與-exec command {} +
-exec本身其實只是在find執行完之後執行另一條命令,我之前以為-exec類似於管道,find找到的檔案會直接被command處理,結果不是這樣;若想要達到管道的效果,需要在 command後面添加“{}”,它代表前面找到的檔案。
然後是文法問題:command後面沒有“{}”時,必須要以一格空格和一個分號結尾;command後面有“{}”時,可以使用前面那種結尾方式,也可以使用一個空格和一個“+”結尾;
為了防止shell本身的擴充,分號需要轉義,要麼前面添加反斜線,要麼用引號括起來;同理“{}”最好也用引號保護起來。
m@meng:~/patches/tmp$ find . -name new -print -exec cat onlyme1 \; ./new haha m@meng:~/patches/tmp$ find . -name new -print -exec cat onlyme1 + find: 缺少“-exec”參數 m@meng:~/patches/tmp$ find . -name new -print -exec cat {} \; ./new hello m@meng:~/patches/tmp$ find . -name new -print -exec cat {} + ./new hello m@meng:~/patches/tmp$ find . -name new -print -exec cat {} ';' ./new hello m@meng:~/patches/tmp$ find . -name new -print -exec cat {} ";" ./new hello
第一個例子說明command和find之間可以是完全獨立的;第二個說明command後面沒有“{}”時只能以分號結尾;後面幾個例子說明了有“{}”時的結尾情況。
- -execdir command ;和-execdir command {} +
與上一對選項類似,只是-exec後面的command是在目前的目錄執行的;而-execdir是在匹配檔案所在子目錄中執行的。我們使用pwd命令測試一下:
m@meng:~/patches/tmp$ lsmagic.mgc new onlyme onlyme1 testm@meng:~/patches/tmp$ ls test/onlyme5 onlyme.shm@meng:~/patches/tmp$ find . -name onlyme.sh -exec pwd \;/home/m/patches/tmpm@meng:~/patches/tmp$ find . -name onlyme.sh -execdir pwd \;/home/m/patches/tmp/test
檔案onlyme.sh在子目錄test中,find在找到它之後執行pwd命令;但是-exec和-execdir的結果是不同的。
- -ok與-okdir
與-exec和-execdir是基本相同的,只是在執行之前會先詢問一下使用者。
- -prune
if the file is a directory, do not descend into it. 就是說,如果某個目錄匹配了,那麼就不再進入這個目錄進行搜尋。
m@meng:~/patches/tmp$ find . -name 'test*' ./test./test/test1m@meng:~/patches/tmp$ find . -name 'test*' -prune ./test
我們看到,匹配 -name ‘test*’的包括test子目錄本身以及test目錄下的test1檔案;但是加上-prune選項之後,就不在進入到test目錄中進行搜尋了,因為test目錄本身也是一
個匹配項。-prune經常配合-path用來排除某些子目錄。
- -quit
立即退出,-quit之後的命令將不再執行。如下:
m@meng:~/patches/tmp$ find . -name new -print -exec cat {} ";"./newhellom@meng:~/patches/tmp$ find . -name new -print -quit -exec cat {} ";"./newm@meng:~/patches/tmp$ find . -name new -print -exec cat {} ";" -quit./newhellom@meng:~/patches/tmp$ find . -name new -quit -print -exec cat {} ";" m@meng:~/patches/tmp$
(三)邏輯運算
先扔一段man手冊的資訊:
The expression is made up of options (which affect overall operation rather than the processing of a specific file, and always return true),tests
(which return a true or false value), and actions (which have side effects and return a true or false value), all separated by operators.
-and is assumed where the operator is omitted.
If the expression contains no actions other than -prune, -print is performed on all files for which the expression is true.
這就是說,expression的三個組成部分其實都是布林運算式,而運算式都是有值的,不是true就是false。手冊說,options的值永遠是true;而其他兩者的值可true可false。
這樣,就會有邏輯運算,其實主要是test之間的運算,因為一個expression裡面可以有多個test,這些test之間可以進行與、或、非運算。
比如上篇文章中的例子:
m@meng:~/patches$ find . -path ./tmp -o -name onlyme./tmp./tmp/onlyme
至此,find命令的基本用法完畢~好累。上面的總結肯定有不完善的地方,我會再以後慢慢補充慢慢糾正的。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。