网站首页 > 技术文章 正文
find中使用正则表达式的语法是:
find dir -regextype type -regex "pattern"
其中:
dir:查找文件的起始目录
-regextype "type":选择使用正则表达式的类型,如下:
type: posix-awk, posix-basic, posix-egrep和posix-extended四种。常用的是后两种。
pattern: find中要想使用正规正则表达式,需要用选项 -regex "pattern"。而pattern就用相应类型风格的正则表达式替换即可。
注意:find的常用选项-name是不支持正则表达式的,充其量只能说-name选项支持通配符 * ? []。
[abc]:表示可以匹配abc中的任意一个
[^abc]:表示不匹配abc,^:表示取反
find . -regextype posix-extended -regex ".*\.[^oa]\>"
表示匹配不以.a或者.o结尾的文件名
1,从目录或者文件中搜索文本字符串
grep -rniw "字符串" "输入目录或文件路径"
选项:
-e, --regexp=PATTERN use PATTERN for matching
-r, --recursive like --directories=recurse
-n, --line-number print line number with output lines
-w, --word-regexp force PATTERN to match only whole words
-i, --ignore-case ignore case distinctions
2,搜索不以".o"结尾的文件名
find . -type f ! -name "*.o"
find . -regextype posix-extended -regex ".*\.[^o]\>"
3,搜索不以".o"或".a"结尾的文件名
find . -type f ! -name "*.o" ! -name "*.a "
find . -regextype posix-extended -regex ".*\.[^oa]\>"
grep -rniw --color=auto "xxx" $(find . -regextype posix-extended -regex ".*\.[^oa]\>")
4,写成脚本的形式+配置别名
(1)新建cgrep.sh脚本文件
#!/bin/sh
dir=`pwd`
if [ $# -eq 2 ]; then
dir=$2
for path in `ls ${dir}`
do
real_path="${dir}/${path}"
if [ -d "${real_path}" ];then
cd "${real_path}" > /dev/null
for file_name in $(find . -regextype posix-extended -regex ".*\.[^oa]\>")
do
if [ -f ${file_name} ];then
grep -rniwH --color=auto $1 ${file_name}
fi
done
#grep -rniw --color=auto $1 $(find . -regextype posix-extended -regex ".*\.[^oa]\>")
cd - > /dev/null
else
grep -rniw --color=auto $1 "${real_path}"
fi
done
else
grep -rniw --color=auto $1 "${dir}"
fi
(2)设置别名cgrep
sudo vim ~/.bashrc
alisa cgrep=路径/cgrep.sh
source ~/.bashrc
(3)在路径下搜索字符串"xxx"
$ cgrep "xxx" "搜索路径"
- 上一篇: 面向开发人员的10个grep命令实用示例
- 下一篇: 「技术干货」一文搞懂Linux内核调试方法(二)
猜你喜欢
- 2024-09-21 memcached/memcache基础(memcache底层实现原理)
- 2024-09-21 关于fastjson出现反序列化远程代码执行漏洞的通知
- 2024-09-21 玩转 Linux 之:磁盘分区、挂载知多少?
- 2024-09-21 Linux sed命令完全攻略(超级详细)
- 2024-09-21 解决Linux内核问题实用技巧之Crash工具结合/dev/mem任意修改内存
- 2024-09-21 实用的一些排查异常方法,你知道几种?
- 2024-09-21 linux之lsof详解(linux lsdev)
- 2024-09-21 Tomcat回显技术学习汇总(tomcat resin)
- 2024-09-21 shell中常用的特殊符号解析(shell中符号的用法)
- 2024-09-21 浅谈Linux三剑客sed命令篇一(linux sed)
- 1514℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 566℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 509℃MySQL service启动脚本浅析(r12笔记第59天)
- 486℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 485℃启用MySQL查询缓存(mysql8.0查询缓存)
- 466℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 445℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 443℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- c语言min函数头文件 (68)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)