网站首页 > 教程分享 正文
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" "搜索路径"
- 上一篇: Linux 离奇磁盘爆满,如何解决?| 原力计划
- 下一篇: 在linux目录行中找空目录
猜你喜欢
- 2025-04-29 3种方法找出哪个进程在监听一个特定的端口
- 2025-04-29 linux搜索命令
- 2025-04-29 Linux使用pidof命令来快速查找进程id
- 2025-04-29 [Linux Shell]简单的文件查找脚本
- 2025-04-29 如何在 Linux 中查找最大的文件?
- 2025-04-29 Linux上使用的文本内容搜索工具--recollgui | 统信 | 麒麟 | 方德
- 2025-04-29 Linux实战之:ldd查找缺失依赖
- 2025-04-29 在 Linux 中查找 IP 地址的 3 种简单方法
- 2025-04-29 在 Linux 中查找系统信息
- 2025-04-29 在linux目录行中找空目录
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- css导航条 (66)
- sqlinsert (63)
- js提交表单 (60)
- param (62)
- parentelement (65)
- jquery分享 (62)
- check约束 (64)
- curl_init (68)
- sql if语句 (69)
- import (66)
- chmod文件夹 (71)
- clearinterval (71)
- pythonrange (62)
- 数组长度 (61)
- javafx (59)
- 全局消息钩子 (64)
- sort排序 (62)
- jdbc (69)
- php网页源码 (59)
- assert h (69)
- httpclientjar (60)
- postgresql conf (59)
- winform开发 (59)
- mysql数字类型 (71)
- drawimage (61)
本文暂时没有评论,来添加一个吧(●'◡'●)