歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux中find命令詳解

linux中find命令詳解

日期:2017/3/1 16:24:21   编辑:關於Linux

http://blog.csdn.net/pipisorry/article/details/39831419

find的語法:

find [起始目錄] 尋找條件 操作

find PATH OPTION [-exec COMMAND { } \;]

因為find命令會根據我們給的option,也就是尋找條件從我們給出的目錄開始對其中文件及其下子目錄中的文件進行遞歸搜索。

尋找條件:

該命令中的尋找條件可以是一個用邏輯運算符 not、and、or 組成的復合條件。

(1) and:邏輯與,在命令中用“-a”表示,是系統缺省的選項,表示只有當所給的條 件都滿足時,尋找條件才算滿足。例如:

find –name ’tmp’ –xtype c -user ’inin’% 該命令尋找三個給定條件都滿足的所有文件

(2) or:邏輯或,在命令中用“-o”表示。該運算符表示只要所給的條件中有一個滿足 時,尋找條件就算滿足。例如:

find –name ’tmp’ –o –name ’mina*’% 該命令查詢文件名為’tmp’或是匹配’mina*’的所有文件。

(3) not:邏輯非,在命令中用“!”表示。該運算符表示查找不滿足所給條件的文件 。例如:

find ! –name ’tmp’% 該命令查詢文件名不是’tmp’的所有文件。

說明:當使用很多的邏輯選項時,可以用括號把這些選項括起來。為了避免Shell本身對括號引起誤解,在話號前需要加轉義字符“\”來去除括號的意義。例:

find \(–name ’tmp’ –xtype c -user ’inin’ \)

查詢條件option參數:

-name ’字串’ 查找文件名匹配所給字串的所有文件,字串內可用通配符 *、?、[ ]。

-lname ’字串’ 查找文件名匹配所給字串的所有符號鏈接文件,字串內可用通配符 *、?、[ ]。

-gid n 查找屬於ID號為 n 的用戶組的所有文件。

-uid n 查找屬於ID號為 n 的用戶的所有文件。

-group ’字串’ 查找屬於用戶組名為所給字串的所有的文件。

-user ’字串’ 查找屬於用戶名為所給字串的所有的文件。

-empty 查找大小為 0的目錄或文件。

-path ’字串’ 查找路徑名匹配所給字串的所有文件,字串內可用通配符*、?、[ ]。

-perm 權限 查找具有指定權限的文件和目錄,權限的表示可以如711,644。

-size n[bckw] 查找指定文件大小的文件,n 後面的字符表示單位,缺省為 b,代表512字節的塊。

-type x 查找類型為 x 的文件,x 為下列字符之一:

b 塊設備文件;c 字符設備文件;d 目錄文件;p 命名管道(FIFO)

f 普通文件;l 符號鏈接文件(symbolic links);s socket文件;-xtype x 與 -type 基本相同,但只查找符號鏈接文件。

以時間為條件查找

-amin n 查找n分鐘以前被訪問過的所有文件。

-atime n 查找n天以前被訪問過的所有文件。

-cmin n 查找n分鐘以前文件狀態被修改過的所有文件。

-ctime n 查找n天以前文件狀態被修改過的所有文件。

-mmin n 查找n分鐘以前文件內容被修改過的所有文件。

-mtime n 查找n天以前文件內容被修改過的所有文件。

-print:將搜索結果輸出到標准輸出。

例子:在root以及子目錄查找不包括目錄/root/bin的,greek用戶的,文件類型為普通文件的,3天之前的名為test-find.c的文件,並將結構輸出,find命令如下:

find / -name "test-find.c" -type f -mtime +3 -user greek -prune /root/bin -print

當然在這其中,-print是一個默認選項,我們不必刻意去配置它

exec選項:

-exec:對搜索的結構指令指定的shell命令。注意格式要正確:"-exec 命令 {} \;"

在}和\之間一定要有空格才行;

{}表示命令的參數即為所找到的文件;命令的末尾必須以“ \;”結束。

例子:對上述例子搜索出來的文件進行刪除操作,命令如下:

find / -name "test-find.c" -type f -mtime +3 -user greek -prune /root/bin -exec rm {} \;

find命令指令實例:

find . - name ‘main*’ - exec more {} \; % 查找當前目錄中所有以main開頭的文件,並顯示這些文件的內容。

find . \(- name a.out - o - name ‘*.o’\)> - atime +7 - exec rm {} \; % 刪除當前目錄下所有一周之內沒有被訪問過的a .out或*.o文件。

% “\(” 和 “\)” 表示括號(),其中的 “\” 稱為轉義符。之所以這樣寫是由於對 Shell 而言,(和)另有不同的含義,而不是這裡的用於組合條件的用途。

% “-name a.out” 是指要查找名為a.out的文件;

% “-name ‘*.o’” 是指要查找所有名字以 .o 結尾的文件。這兩個 -name 之間的 -o 表示邏輯或(or),即查找名字為a.out或名字以 .o結尾的文件。

% find命令在當前目錄及其子目錄下找到這佯的文件之後,再進行判斷,看其最後訪問時間 是否在7天以前(條件 -atime +7),若是,則對該文件執行命令 rm(-exec rm {} \;)。

其中 {} 代表當前查到的符合條件的文件名,\;則是語法所要求的。

% 上述命令中第一行的最後一個 \ 是續行符。當命令太長而在一行寫不下時,可輸入一個 \,之後系統將顯示一個 >,指示用戶繼續輸入命令。

問題:

linux中find命令查找時不包含某些目錄

find 命令忽略某個或多個子目錄的方法

解決方案:

在linux中用find 進行查找的時候,有時候需要忽略某些目錄不查找,可以使用 -prune 參數來進行過濾,要忽略的路徑參數必須緊跟著搜索的路徑之後,否則該參數無法起作用。

man find

...
-path pattern
              File  name matches shell pattern pattern.  The metacharacters do
              not treat `/' or `.' specially; so, for example,
                        find . -path "./sr*sc"
              will print an entry for a directory called `./src/misc' (if  one
              exists).   To  ignore  a whole directory tree, use -prune rather
              than checking every file in the tree.  For example, to skip  the
              directory  `src/emacs'  and  all files and directories under it,
              and print the names of the other files found, do something  like
              this:
                        find . -path ./src/emacs -prune -o -print
              Note that the pattern match test applies to the whole file name,
              starting from one of the start points named on the command line.
              It  would  only  make sense to use an absolute path name here if
              the relevant start point is also an absolute path.   This  means
              that this command will never match anything:
                        find bar -path /foo/bar/myfile -print
              The  predicate -path is also supported by HP-UX find and will be
              in a forthcoming version of the POSIX standard.
...
也可以使用參數-wholename,不過不建議了
 -wholename pattern
              See -path.    This alternative is less portable than -path.
-prune 使用這一選項可以使find命令不在當前指定的目錄中查找,如果同時使用-depth選項,那麼-prune將被find命令忽略。

還要注意 \( 前後都有空格

eg:

root@ubuntu:/tmp1#find ./ -type f    #/tmp1目錄下所有文件夾裡面的所有文件
./file
./1/1.cpp
./2/2.cpp
root@ubuntu:/tmp1#find ./ -path ./1 -prune -o -type f -print    #/tmp1中查找文件但忽略文件夾/1中的文件
./file
./2/2.cpp
root@ubuntu:/tmp1#find ./ \( -path ./1 -o -path ./2 \) -prune -o -type f -print    #/tmp1中查找文件同時忽略文件夾/1 /2中的文件
./file

針對文件模式“./1”使用 -path(或者也可以-wholename) 測試,如果該模式已找到,-prune 可防止 find 下到該目錄中。

find ./ -path ./1 -prune -o -type f -print
find [-path ..] [expression] 在路徑列表的後面的是表達式
-path ./1 -prune -o -print 是 -path ./1 -a -prune -o
布爾類型“-o”使 find 可以針對其他目錄處理該命令的其余部分。由於每個表達式之間有一個假設的隱式 and 運算符 (-a),因此,如果左側的表達式計算結果為 false,and 之後的表達式將不進行計算;因此需要 -o 運算符。
【詳細解釋:-print 的簡寫表達式按順序求值, -a 和 -o 都是短路求值,與 shell 的 && 和 || 類似如果 -path ./1為真,則 求值-prune , -prune 返回真,與邏輯表達式... -a -prune為真;否則不求值 -prune,與邏輯表達式為假。
如果 -path ./1 -a -prune 為假,則求值 -print ,-print返回真,或邏輯表達式為真;否則不求值 -print,或邏輯表達式為真。】這個表達式組合用偽碼可以寫為 if -path ./1 then
-prune
else
-print
find忽略多個文件夾 find ./ \( -path ./1 -o -path ./2 \) -prune -o -type f-name "*" -print
圓括號表示表達式的結合。 \ 表示引用,即指示 shell 不對後面的字符作特殊解釋,而留給 find 命令去解釋其意義。
查找某一確定文件,-name等選項加在-o 之後
eg:統計我寫了多少行c/c++代碼:
root@ubuntu:/media/E/mine/C++#find ./ \( -path ./SvdFeature -o -path ./SvdFeatureDemo -o -path ./SvdFeatureInfer -o -path ./testSVD \) -prune -o \( -name '*.c' -o -name '*.cpp' -o -name '*.h' -type f \) -print | xargs wc -l | more
...
16867 總用量

from:http://blog.csdn.net/pipisorry/article/details/39831419

ref:

Linux文件查找命令find

linux中Find命令的使用

linux find 命令忽略某個或多個子目錄的方法

Copyright © Linux教程網 All Rights Reserved