Linux中如何按关键字搜索文本文件
问题描述
在Linux中,有时候想指定关键字或规则匹配搜索指定目录及其子目录中的所有文本文件,并打印出匹配的文件和对应的行号,使用grep命令可以进行递归搜索。
使用grep命令进行搜索
grep -rnw /path/to/somewhere/ -e pattern
参数说明:
-r or -R is recursive (表示递归搜索所有子目录)
-n is line number (输出结果是否打印行号)
-w stands for match the whole word. (是否整字匹配)
-l (lower-case L) can be added to just give the file name of matching files.
-e is the pattern used during the search (搜索匹配规则)
使用 --exclude, --include, --exclude-dir 选项可以指定包含或排除文件或目录
如下命令表示只搜索 .c or .h 文件:
grep --include=\*.{c,h} -rnw /path/to/somewhere/ -e pattern
以下命令表示排除.o类型的文件:
grep --exclude=\*.o -rnw /path/to/somewhere/ -e pattern
对于目录搜索,可以使用 --exclude-dir 排除多个目录。例如,以下命令会排除 dir1/, dir2/ 整个目录,以及所有匹配*.dst的目录
grep --exclude-dir={dir1,dir2,*.dst} -rnw /path/to/somewhere/ -e pattern
更新于:1个月前赞一波!
相关文章
- linux shell的几种截取字符串的方法
- Linux中date命令如何格式化输出需要的时间格式
- 使用Docker Wine Qemu KVM在Linux运行Windows应用
- Debian Linux国内常用镜像源
- CentOS7安装unzip解压工具命令使用方法
- Linux使用Docker部署.NET6网站图文教程
- Linux中通过命令连接指定WiFi
- 什么是宝塔Linux
- Linux 删除文件或目录 rm 命令
- 使用 Python 拆分文本文件的最快方法是什么?
- 怎样在Linux系统 Ubuntu18.04 中安装微信
- 下载文本文件javascript
- C# WebSocket Client在linux连接失败
- .NET部署到Linux和Windows哪个性能更高?
- CentOS 7 Linux系统中添加新用户并给其授权
- Linux中如何释放DHCP获取的IP地址并重新获取
- Linux命令大全
- Linux RPM包安装指南
- 解决Linux下无法卸载繁忙文件系统的方法
- Linux磁盘空间管理:释放空间、避免硬盘满、提高性能
文章评论
全部评论