Linux 下 ag 命令的使用

[toc]


一、概述

1、功能

在 Linux 系统中,ag 命令是用于文本搜索的高效工具。它具有快速、灵活的搜索功能,并支持正则表达式匹配。

2、下载

在下面是各个系统安装 ag 的命令:

1
sudo apt-get install silversearcher-ag
  1. MacOS:
1
brew install the_silver_searcher
  1. Debian/Ubuntu:

    1
    sudo apt-get install silversearcher-ag
  2. CentOS:

    1
    sudo yum install the_silver_searcher
  3. Fedora:

    1
    sudo dnf install the_silver_searcher
  4. Arch:

    1
    sudo pacman -S the_silver_searcher

二、常用参数说明

ag 目录的语法格式如下:

1
ag [选项] [搜索模式] [文件/目录]

下面是一些常用的参数:

  • -i:忽略大小写进行搜索;
  • -w:只匹配整个单词,而不是部分匹配;
  • -r:递归搜索指定目录下的文件;
  • -l:只列出匹配的文件名;
  • -c:统计匹配的文件数;
  • -n:显示行号。

更多参数信息,可以使用命令:ag -h 查看。

三、实例

  1. 在指定目录下查询字符串
1
ag pattern /path/to/directory

pattern 是要搜索的字符串,/path/to/directory 是要搜索的目录。如果省略了目录信息,则表示在当前目录及其子目录下搜索包含 pattern 的文件。

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[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 preallocate for dirs */
485:#define EXT2_FEATURE_COMPAT_DIR_PREALLOC 0x0001

include/bb_archive.h
222:void dealloc_bunzip(bunzip_data *bd) FAST_FUNC;

include/inet_common.h
26:/* These return malloced string */
...
## 篇幅原因,只展示部分代码

该命令结果首先会打印出相应的文件目录,下面紧接的是匹配到的段落:左边的数字是该匹配到的文本出现在对应文件的行数;右边则是对应的文本。

  1. 搜索指定文件类型的文件
1
ag pattern --hh

其中 --hh 选项指定要搜索的文件类型,上例中搜索的是 C 语言中的 .h 头文件。当然也可以用 --cc 或者 --cpp 选项,都包含的和 C 相关的各个文件。

通过使用 --list-file-types 选项,可以查看 ag 命令支持的文件类型列表:

1
2
3
4
5
6
7
8
9
10
11
12
13
[projectsauron]:~/linux/nfs/busybox-1.29.0$ ag --list-file-types
The following file types are supported:
## 篇幅原因,只展示部分代码
...
--cc
.c .h .xs

--cpp
.cpp .cc .C .cxx .m .hpp .hh .h .H .hxx .tpp

--hh
.h
...
  1. 搜索时忽略指定文件或目录:
1
ag pattern --ignore-file=/path/to/ignore

--ignore-file 选项指定要忽略的文件或目录,上例中忽略了 /path/to/ignore 目录。

  1. 利用正则表达式限制文件类型

例如,以下命令限制只在 .c 文件中搜索:

1
[projectsauron]:~/linux/nfs/busybox-1.29.0$ ag -G ".+\.c" "config"
  1. 对结果取反

如果想要搜索不满足特定模式的行,用 ag -v 对搜索结果取反。

  1. 统计匹配的文件数

ag -c 可以统计各个文件匹配到目标字符串的次数:

1
2
3
4
5
6
7
[projectsauron]:~/linux/nfs/busybox-1.29.0$ ag -c "alloc" ./include
include/bb_e2fs_defs.h:7
include/dump.h:1
include/inet_common.h:1
include/bb_archive.h:1
include/platform.h:6
include/libbb.h:97
  1. 统计匹配的文件

ag -c 类似,不过 ag -l 只会列出匹配到的文件:

1
2
3
4
5
6
7
[projectsauron]:~/linux/nfs/busybox-1.29.0$ ag -c "alloc" ./include
include/bb_e2fs_defs.h
include/dump.h
include/inet_common.h
include/bb_archive.h
include/platform.h
include/libbb.h

四、帮助文档

如下是 ag -h 的打印信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
[projectsauron]:~/linux/nfs/busybox-1.29.0$ ag -h
用法: ag [FILE-TYPE] [OPTIONS] PATTERN [PATH]

递归地在 PATH 中搜索 PATTERN.
Like grep or ack, but faster.

例子:
ag -i foo /bar/

输出选项:
--ackmate 打印结果为 Ackmate 可解析的格式
-A --after [LINES] 打印匹配后的行 (默认值:2)
-B --before [LINES] 打印匹配前的行 (默认值:2)
--[no]break 打印不同文件中匹配项之间的换行符 (默认开启)
-c --count 只打印每个文件中匹配项的数量. (这通常与匹配行的数量不同)
--[no]color 在结果中打印颜色代码 (默认启用)
--color-line-number 行号的颜色代码 (Default: 1;33)
--color-match 结果匹配数字的颜色代码 (Default: 30;43)
--color-path 路径名称的颜色代码 (Default: 1;32)
--column 在结果中打印列号
--[no]filename 打印文件名 (启用,除非搜索单个文件)
-H --[no]heading 在每个文件匹配结果前打印文件名 (默认开启)
-C --context [LINES] 打印匹配前后的行 (默认值:2)
--[no]group 和 --[no]break --[no]heading 相同
-g --filename-pattern PATTERN 打印匹配 PATTERN 的文件名
-l --files-with-matches 只打印包含匹配项的文件名 (不要打印匹配的行)
-L --files-without-matches 只打印不包含匹配项的文件名
--print-all-files 打印所有搜索文件的标题,即使是那些不包含匹配项的文件
--[no]numbers 打印行号。默认是当搜索流时省略行号
-o --only-matching 只打印匹配的行
--print-long-lines 在非常长的行上打印匹配 (默认: >2k 字符)
--passthrough 搜索流时,打印所有行,即使它们不匹配
--silent 屏蔽所有日志信息,包括错误信息
--stats 打印统计数据(扫描的文件,花费的时间等)
--stats-only 打印统计信息,不打印其他信息
(搜索单个文件时和 --count 相同)
--vimgrep 打印类似 vim 的 :vimgrep /pattern/g
(它会报告行上的每个匹配项)
-0 --null --print0 用 null 分隔文件名 (对于 'xargs -0')

搜索选项:
-a --all-types 搜索所有文件(不包括隐藏文件或忽略文件中的模式)
-D --debug Ridiculous debugging (可能没有用)
--depth NUM 搜索最多 NUM 个目录深度(默认值:25)
-f --follow Follow symlinks
-F --fixed-strings Alias for --literal for compatibility with grep
-G --file-search-regex PATTERN 将搜索限制为文件名匹配模式
--hidden 搜索隐藏文件 (obeys .*ignore files)
-i --ignore-case 不区分大小写
--ignore PATTERN 忽略匹配模式的文件/目录
(literal file/directory names also allowed)
--ignore-dir NAME Alias for --ignore for compatibility with ack.
-m --max-count NUM 在 NUM 匹配后跳过文件的其余部分 (默认:10 000)
--one-device 不要通过链接到其他设备.
-p --path-to-ignore STRING Use .ignore file at STRING
-Q --literal 不将 PATTERN 解析为正则表达式
-s --case-sensitive 匹配区分大小写
-S --smart-case 不区分大小写,除非 PATTERN 包含大写字符(默认启用)
--search-binary 在二进制文件中查找匹配项
-t --all-text 搜索所有文本文件(不包括隐藏文件)
-u --unrestricted 不加限制地搜索所有文件(ignore .ignore, .gitignore, etc.;
也可以搜索二进制文件和隐藏文件)
-U --skip-vcs-ignores Ignore VCS ignore files
(.gitignore, .hgignore; still obey .ignore)
-v --invert-match
-w --word-regexp 仅匹配整个单词
-W --width NUM 以 NUM 个字符截断匹配的行
-z --search-zip 搜索压缩文件(例如gzip)的内容

文件类型:
搜索可以被限制为某些类型的文件。例如:
ag --html needle
- 在后缀为 .htm、.html、.shtml 或 .xhtml 的文件中搜索 needle 。

要获取支持的文件类型列表,请运行:
ag --list-file-types

ag最初由Geoff Greer创建。更多信息(和最新版本)可以查询 http://geoff.greer.fm/ag


Linux 下 ag 命令的使用
http://example.com/2024/08/05/ag/
作者
Yu xin
发布于
2024年8月5日
许可协议