一、概述
1、功能
在 Linux 系统中,ag 命令是用于文本搜索的高效工具。它具有快速、灵活的搜索功能,并支持正则表达式匹配。
2、下载
在下面是各个系统安装 ag 的命令:
sudo apt-get install silversearcher-ag
- MacOS:
brew install the_silver_searcher
- Debian/Ubuntu:
sudo apt-get install silversearcher-ag
- CentOS:
sudo yum install the_silver_searcher
- Fedora:
sudo dnf install the_silver_searcher
- Arch:
sudo pacman -S the_silver_searcher
二、常用参数说明
ag 目录的语法格式如下:
ag [选项] [搜索模式] [文件/目录]
下面是一些常用的参数:
- -i:忽略大小写进行搜索;
- -w:只匹配整个单词,而不是部分匹配;
- -r:递归搜索指定目录下的文件;
- -l:只列出匹配的文件名;
- -c:统计匹配的文件数;
- -n:显示行号。
更多参数信息,可以使用命令:ag -h
查看。
三、实例
- 在指定目录下查询字符串
ag pattern /path/to/directory
pattern
是要搜索的字符串,/path/to/directory
是要搜索的目录。如果省略了目录信息,则表示在当前目录及其子目录下搜索包含 pattern 的文件。
例如:
[projectsauron]:~/linux/nfs/busybox-1.29.0$ ag "alloc" ./include
include/dump.h
52:dumper_t* alloc_dumper(void) FAST_FUNC;
include/bb_e2fs_defs.h
406: * Performance hints. Directory preallocation should only
407: * happen if the EXT2_FEATURE_COMPAT_DIR_PREALLOC flag is on.
409: uint8_t s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
410: uint8_t s_prealloc_dir_blocks; /* Nr to