Find and Grep

find

1.格式

Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

  default path is the current directory;

  default expression is -print

expression may consist of: operators, options, tests, and actions

   find [path...] [operators,options,tests,actions]

2.参数

operators (decreasing precedence; -and is implicit where no others are given):

      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2       EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true):

   -daystart -follow -regextype

normal options (always true, specified before other expressions):

 -depth

--help

-maxdepth LEVELS

-mindepth LEVELS

-mount -noleaf      

--version

-xdev

-ignore_readdir_race

-noignore_readdir_race

tests (N can be +N or -N or N):

-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]    

 

actions:  

-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 解决

grep

1.格式

Usage:grep [OPTION]... PATTERN [FILE]...

Search for PATTERN in each FILE or standard input. PATTERN is, by default, a basic regular expression (BRE).

2.参数

Regexp selection and interpretation 正则表达式选择与说明:  

-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

Miscellaneous 其他控制参数:  

-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

Output control 输出控制参数:  

-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

Context control 上下文控制参数:  

-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 的行。

 

posted @ 2016-10-13 10:16  Qiengo  阅读(661)  评论(0编辑  收藏  举报