Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]linux
default path is the current directory;正則表達式
default expression is -printexpress
expression may consist of: operators, options, tests, and actionsbash
find [path...] [operators,options,tests,actions]less
( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2 EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2socket
-daystart -follow -regextype函數
-depthui
--helpthis
-maxdepth LEVELSunix
-mindepth LEVELS
-mount -noleaf
--version
-xdev
-ignore_readdir_race
-noignore_readdir_race
-amin N | -anewer FILE | -atime N,文件訪問時間 | -cmin N |
-cnewer FILE | -ctime N,文件建立時間 | -empty | -false |
-fstype TYPE | -gid N | -group NAME | -ilname PATTERN |
-iname PATTERN | -inum N | -iwholename PATTERN | -iregex PATTERN |
-links N | -lname PATTERN | -mmin N | -mtime N,文件更改時間 |
-name PATTERN,名字 | -newer FILE | -nouser,no user | -nogroup, no group |
-path PATTERN | -perm [+-]MODE,權限 | -regex PATTERN | -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 ; 對匹配的文件執行COMMAND命令。相應命令的形式爲'command' { } \;,注意{ }和\;之間的空格。
-exec COMMAND {} + -ok COMMAND ; ok 和-exec相同,只不過在操做前要詢用戶
-execdir COMMAND ;
-execdir COMMAND {} + -okdir COMMAND ;
若是什麼參數也不加,find默認搜索當前目錄及其子目錄,而且不過濾任何結果(也就是返回全部文件),將它們全都顯示在屏幕上。
find的使用實例:
$ find . -name 'my*'
搜索當前目錄(含子目錄,如下同)中,全部文件名以my開頭的文件。
$ find . -name 'my*' -ls
搜索當前目錄中,全部文件名以my開頭的文件,並顯示它們的詳細信息。
$ find . -type f -mmin -10
搜索當前目錄中,全部過去10分鐘中更新過的普通文件。若是不加-type f參數,則搜索普通文件+特殊文件+目錄。
用例:
//從根目錄下開始查找abc.cpp文件,無錯誤輸出 find / -name abc.cpp 2>/dev/null //在當前目錄下全部.cpp文件中查找efg函數,xargs展開find得到的結果,使其做爲grep的參數 find . -name "*.cpp" | xargs grep 'efg' //刪除當前目錄下全部.cpp文件 find . -name "*.cpp" | xargs rm
另外 rm mv等命令對大量文件操做是報錯 -bash: /bin/rm: Argument list too long 也可用xargs 解決
Usage:grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input. PATTERN is, by default, a basic regular expression (BRE).
-E, --extended-regexp PATTERN is an extended regular expression (ERE)
-F, --fixed-strings PATTERN is a set of newline-separated fixed strings
-G, --basic-regexp PATTERN is a basic regular expression (BRE)
-P, --perl-regexp PATTERN is a Perl regular expression
-e, --regexp=PATTERN use PATTERN for matching
-f, --file=FILE obtain PATTERN from FILE
-i, --ignore-case ignore case distinctions 忽略大小寫
-w, --word-regexp force PATTERN to match only whole words 只匹配整個單詞,而不是字符串的一部分(如匹配’magic’,而不是’magical’),
-x, --line-regexp force PATTERN to match only whole lines
-z, --null-data a data line ends in 0 byte, not newline
-s, --no-messages suppress error messages 忽略錯誤信息
-v, --invert-match select non-matching lines
-V, --version print version information and exit
--help display this help and exit
--mmap deprecated no-op; evokes a warning
-m, --max-count=NUM stop after NUM matches
-b, --byte-offset print the byte offset with output lines
-n, --line-number print line number with output lines 輸出行號
--line-buffered flush output on every line
-H, --with-filename print the file name for each match
-h, --no-filename suppress the file name prefix on output
--label=LABEL use LABEL as the standard input file name prefix
-o, --only-matching show only the part of a line matching PATTERN
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE; TYPE is 'binary', 'text', or 'without-match'
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories; ACTION is 'read', 'recurse', or 'skip' 搜索項中目錄的處理方式,recurse遞歸搜索
-D, --devices=ACTION how to handle devices, FIFOs and sockets; ACTION is 'read' or 'skip' 搜索項中設備文件的處理方式
-r, --recursive like --directories=recurse 遞歸搜索,等同於 -d recurse參數
-R, --dereference-recursive likewise, but follow all symlinks 遞歸搜索,可是能夠添加搜索選項
--include=FILE_PATTERN search only files that match FILE_PATTERN 只搜索匹配FILE_PATTERN的文件
--exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN 忽略匹配FILE_PATTERN的文件和目錄
--exclude-from=FILE skip files matching any file pattern from FILE 忽略匹配從FILE中獲取的FILE_PATTERN的文件
--exclude-dir=PATTERN directories that match PATTERN will be skipped. 忽略匹配FILE_PATTERN的目錄
-L, --files-without-match print only names of FILEs containing no match 只輸出不包含匹配字符的文件名
-l, --files-with-matches print only names of FILEs containing matches 只輸出包含匹配字符的文件名
-c, --count print only a count of matching lines per FILE 只輸出每一個文件中匹配行的計數
-T, --initial-tab make tabs line up (if needed)
-Z, --null print 0 byte after FILE name
-B, --before-context=NUM print NUM lines of leading context 顯示匹配的前NUM行
-A, --after-context=NUM print NUM lines of trailing context 顯示匹配的後NUM行
-C, --context=NUM print NUM lines of output context 顯示匹配的先後NUM行
-NUM same as --context=NUM
--color[=WHEN],
--colour[=WHEN] use markers to highlight the matching strings; WHEN is 'always', 'never', or 'auto'
-U, --binary do not strip CR characters at EOL (MSDOS/Windows)
-u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS/Windows)
pattern正則表達式主要參數:
\: 忽略正則表達式中特殊字符的原有含義。
^:匹配正則表達式的開始行。
$: 匹配正則表達式的結束行。
\<:從匹配正則表達 式的行開始。
\>:到匹配正則表達式的行結束。
[ ]:單個字符,如[A]即A符合要求 。
[ - ]:範圍,如[A-Z],即A、B、C一直到Z都符合要求 。
。:全部的單個字符。
* :有字符,長度能夠爲0。
默認狀況下,‘grep’只搜索當前目錄。若是此目錄下有許多子目錄,‘grep’會以以下形式列出:
grep: sound: Is a directory
這可能會使‘grep’的輸出難於閱讀。這裏有兩種解決的辦法:
grep -r 或 grep -d recurse:遞歸搜索子目錄
grep -d skip:忽略子目錄
固然,若是預料到有許多輸出,您能夠經過 管道 將其轉到‘less’上閱讀:
$ grep magic /usr/src/linux/Documentation/* | less
有一點要注意,您必需提供一個文件過濾方式(搜索所有文件的話用 *)。若是您忘了,‘grep’會一直等着,直到該程序被中斷。若是您遇到了這樣的狀況,按 <CTRL c> ,而後再試。
grep pattern1 | pattern2 files :顯示匹配 pattern1 或 pattern2 的行,
grep pattern1 files | grep pattern2 :顯示既匹配 pattern1 又匹配 pattern2 的行。
這裏還有些用於搜索的特殊符號:
\< 和 \> 分別標註單詞的開始與結尾。
例如:
grep man * 會匹配 ‘Batman’、‘manic’、‘man’等,
grep ‘\<man’ * 匹配‘manic’和‘man’,但不是‘Batman’,
grep ‘\<man\>’ 只匹配‘man’,而不是‘Batman’或‘manic’等其餘的字符串。
‘^’:指匹配的字符串在行首,
‘$’:指匹配的字符串在行尾,
用例:
$ grep ‘test’ d* 顯示全部以d開頭的文件中包含 test的行。也可使用cat d* | grep 'test' $ grep ‘test’ aa bb cc 顯示在aa,bb,cc文件中匹配test的行。 $ grep "dma" * -nR --include=*.dts* 在匹配*.dts*的文件中查找dma,若是使用 grep "dma" *.dts* -nR則只會查找本級目錄 $ grep "dma" * -nR | grep -v "case" 遞歸查找包含dma可是不包含case的文本 $ grep ‘[a-z]\{5\}’ aa 顯示全部包含每一個字符串至少有5個連續小寫字符的字符串的行。 $grep ‘w\(es\)t.*\1′ aa 若是west被匹配,則es就被存儲到內存中,並標記爲1,而後搜索任意個字符(.*),這些字符後面緊跟着 另一個es(\1),找到就顯示該行。若是用egrep或grep -E,就不用」\」號進行轉義,直接寫成’w(es)t.*\1′就能夠了。 # grep '^root' /etc/group 匹配正則表達式的開始行 root::0:root # grep 'root$' /etc/group 匹配正則表達式的結束行 root::0:root mail::6:root grep pattern1 | pattern2 files :顯示匹配 pattern1 或 pattern2 的行, grep pattern1 files | grep pattern2 :顯示既匹配 pattern1 又匹配 pattern2 的行。