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 的行。
Find and Grep的更多相关文章
- grep 查找bash脚本中的注释代码
出于安全性的考虑,不建议在bash脚本中注释掉不使用的代码.也就是说如果某段代码不使用了,那么应该删除掉,而不是简单地注释掉.假如你突然意识到这一点,而以前并没有遵从这个原则,现在需要找出脚本中的注释 ...
- linux grep命令
linux grep命令1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressio ...
- linux中grep的应用
h3 { color: rgb(255, 255, 255); background-color: rgb(30,144,255); padding: 3px; margin: 10px 0px } ...
- Linux命令-文件文本操作grep
文件文本操作 grep 在文件中查找符合正则表达式条件的文本行 cut 截取文件中的特定字段 paste 附加字段 tr 字符转换或压缩 sort 调整文本行的顺序,使其符合特定准则 uniq 找出重 ...
- 4-4 grep及正则表达式
1. grep:Globally search a Regular Expression and Print:根据模式搜索文本,并将符合模式的文本行显示出来 pattern:文本字符和正则表达式的元字 ...
- grep 命令过滤配置文件中的注释和空行
grep 用法 Usage: grep [OPTION]... PATTERN [FILE]... Search for PATTERN in each FILE or standard input. ...
- sed awk grep三剑客常用
sed的常用用法: awk的常用用法: grep的常用用法: 除了列出符合行之外,并且列出后10行. grep -A 10 Exception kzfinance-front.log 除了列出符合行之 ...
- awk命令和grep命令的使用
1.遇到需求:用ping命令去检测系统网络延迟 跑 ping baidu.com -c 3,想要直接得到平均延迟. ping baidu.com -c 3 | grep rtt | awk -F \/ ...
- grep 命令
简单介绍:grep命令是用于分析一行信息,若当中有我们所需要的信息,就将该行取出来. 语法结构:grep [-acinv] [--color=auto] '查找关键字' #{filename} -a: ...
- 使用git grep进行git搜索
1.git grep foo 会自动map所有包含foo的文件 2.git grep -n foo 显示行号 3.git grep --name-only foo 只显示文件名 4.git grep ...
随机推荐
- Decorator Wrapper 装饰模式 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 只用CSS做到完全居中
我们都知道 margin:0 auto; 的样式能让元素水平居中,而 margin: auto; 却不能做到垂直居中……直到现在.但是,请注意!想让元素绝对居中,只需要声明元素高度,并且附加以下样式, ...
- 让ie6(opera)支持微软雅黑字体
一.让IE6支持微软雅黑,添加一句声明: <html lang="zh-CN"> 在网页的HTML标签内加入红色部分的声明,就可以了. 二.让Opera浏览器支持微软 ...
- Android -- onWindowFocusChanged
Android中获取手机屏幕的高度和宽度,我们知道在onCreate方法中获取到的值都是为0的,有人说可以在onClick方法中获取值,这个也是个方法 ,但在onWindowFocusChanged方 ...
- 【Python】Django删除数据迁移记录
find . -path "*migrations*" -name "*.py" -not -path "*__init__*" -exec ...
- 【转】NativeScript的工作原理:用JavaScript调用原生API实现跨平台
原文:https://blog.csdn.net/qq_21298703/article/details/44982547 -------------------------------------- ...
- C#.NET常见问题(FAQ)-VS如何整个项目中查找字符串
Ctrl+F打开查找对话框,然后输入查找字符串,电机右边的小三角,选择整个解决方案,就可以遍历所有文件查找指定字符了 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http:// ...
- Office PPT如何切换到返回幻灯片
1 如图所示,有"老师""同学""家人"三个板块,如果依次播放,将播放"老师1" "老师2" &qu ...
- sse float 转int 截断和不截断
之前, 我用sse指令, 想把float 型转成int, 不过其中遇到了一些困惑,就是截断和不截断的问题, 这个问题一直困扰我好集体, 最后终于解决了, 原来sse本身就有截断和不截断的指令. _mm ...
- UITabBarController 的配置
UITabBarController --> UITabBar Customizing Appearance backgroundImage 背景图片 selectedImageTintCol ...