Create New Document

The title of your document (will be displayed as H1)
URL-friendly name (no spaces, use dashes)
Path where to create document (optional, use forward slashes to create subdirectories)

Move/Rename Document

Current location of the document
New path for the document (including the slug)
This only changes the document's path. It does not modify the document's title (H1 heading).

Delete Document

Are you sure you want to delete this document? This action cannot be undone.

Warning: If this is a folder, all contents including subfolders and documents will be deleted.

Message

Message content goes here.

Confirm Action

Are you sure?

Attachments

Allowed file types: jpg, jpeg, png, gif, svg, webp, txt, log, csv, sfd, zip, pdf, docx, xlsx, pptx, mp4 (Max: 10MB)

Document Files

Loading attached files...

Document History

Previous Versions

Loading versions...

Preview

Select a version to preview

Wiki Settings

Language for the user interface
Number of versions to keep per document. Set to 0 to disable versioning.
Maximum allowed file size for uploads in MB.

User Management

Add New User

Leave empty to keep current password
Users with these groups can access restricted sections.

Define path-based access rules for sections of your wiki, then assign users to groups in the Users tab. Rules are evaluated in order. First match wins.

Active Rules

Import markdown files from a ZIP archive. Files will be processed and stored in the appropriate document structure. Directory structure in the ZIP (category/subcategory) will be preserved in the wiki.

Upload a ZIP file containing markdown (.md) files to import.

Create and manage backups of your wiki data. Backups include all documents, images, and configuration files.

Available Backups

Loading backups...

Add/Edit Access Rule

Selected: /

Add Column

Banner

grep

grep 命令完全指南

grep 是 Linux 中最强大的文本搜索工具,用于在文件中搜索匹配特定模式的行。名称来源于 global regular expression print(全局正则表达式打印)。


一、命令语法

grep [选项]... 模式 [文件]...

基本用法

grep "pattern" file.txt          # 在文件中搜索
command | grep "pattern"         # 从管道输入搜索
grep "pattern" file1 file2       # 在多个文件中搜索

二、选项详解

1. 模式选择与解释

选项 说明
-E, --extended-regexp 使用扩展正则表达式(ERE)
-F, --fixed-strings 模式作为固定字符串(不解释正则)
-G, --basic-regexp 使用基本正则表达式(BRE,默认)
-P, --perl-regexp 使用 Perl 兼容正则表达式(PCRE)
-e, --regexp=PATTERNS 指定模式(可多次使用)
-f, --file=FILE 从文件读取模式(每行一个)
-i, --ignore-case 忽略大小写
--no-ignore-case 不忽略大小写(默认)
-w, --word-regexp 匹配整个单词
-x, --line-regexp 匹配整行
-z, --null-data 以 NUL 字节作为行结束符

2. 输出控制

选项 说明
-m, --max-count=NUM 匹配 NUM 行后停止
-b, --byte-offset 显示字节偏移量
-n, --line-number 显示行号
--line-buffered 每行输出后刷新缓冲区
-H, --with-filename 显示文件名(多文件时默认)
-h, --no-filename 不显示文件名(单文件时默认)
--label=LABEL 为标准输入设置标签
-o, --only-matching 只显示匹配的部分
-q, --quiet, --silent 不输出任何内容(仅返回状态码)
--binary-files=TYPE 处理二进制文件:binarytextwithout-match
-a, --text 将二进制文件当作文本处理
-I 忽略二进制文件
-d, --directories=ACTION 处理目录:readrecurseskip
-D, --devices=ACTION 处理设备文件:readskip
-r, --recursive 递归搜索目录
-R, --dereference-recursive 递归搜索并跟随符号链接
--include=GLOB 只搜索匹配 GLOB 的文件
--exclude=GLOB 跳过匹配 GLOB 的文件
--exclude-from=FILE 从文件读取要跳过的模式
--exclude-dir=GLOB 跳过匹配 GLOB 的目录
-L, --files-without-match 只显示不匹配的文件名
-l, --files-with-matches 只显示匹配的文件名
-c, --count 只显示匹配行数
-T, --initial-tab 使制表符对齐
-Z, --null 文件名后输出 NUL 字节

3. 上下文控制

选项 说明
-B, --before-context=NUM 显示匹配行前 NUM 行
-A, --after-context=NUM 显示匹配行后 NUM 行
-C, --context=NUM 显示匹配行前后各 NUM 行
-NUM 等同于 --context=NUM
--group-separator=SEP 匹配组之间的分隔符
--no-group-separator 不显示匹配组分隔符
--color[=WHEN] 高亮匹配字符串:alwaysneverauto

4. 杂项

选项 说明
-s, --no-messages 抑制错误信息
-v, --invert-match 显示不匹配的行
-V, --version 显示版本信息
--help 显示帮助信息
-U, --binary 不剥离 CR 字符(Windows 文件)

三、常用案例

基础搜索

1. 在文件中搜索字符串

grep "error" /var/log/syslog          # 搜索 error
grep "Failed password" /var/log/auth.log  # 搜索登录失败

2. 忽略大小写

grep -i "warning" log.txt             # 匹配 WARNING, Warning, warning 等
grep -i "hello world" file.txt

3. 显示行号

grep -n "error" log.txt               # 显示匹配行的行号
# 输出:15:Error: Connection timeout

4. 显示匹配行的上下文

grep -B 2 "error" log.txt             # 显示匹配行前2行
grep -A 3 "error" log.txt             # 显示匹配行后3行
grep -C 5 "error" log.txt             # 显示匹配行前后各5行
grep -B 2 -A 3 "error" log.txt        # 前2后3

5. 统计匹配行数

grep -c "error" log.txt               # 统计匹配行数
grep -c -i "warning" log.txt

6. 显示不匹配的行

grep -v "INFO" log.txt                # 显示不包含 INFO 的行
grep -v "^$" file.txt                 # 删除空行
grep -v "#" config.conf | grep -v "^$"  # 去除注释和空行

7. 只显示匹配部分

grep -o "error" log.txt               # 只显示 error 这个词
grep -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" log.txt  # 提取 IP 地址

8. 搜索多个文件

grep "error" *.log                    # 搜索所有 .log 文件
grep "pattern" file1.txt file2.txt    # 搜索多个指定文件

9. 递归搜索目录

grep -r "TODO" /project/src/          # 递归搜索
grep -r -n "FIXME" .                  # 递归搜索并显示行号
grep -R "pattern" /path/              # 递归并跟随符号链接

正则表达式搜索

10. 使用扩展正则表达式

# 搜索数字
grep -E "[0-9]+" file.txt

# 搜索邮箱
grep -E "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" file.txt

# 搜索 IP 地址
grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" file.txt

# 匹配多个模式
grep -E "error|warning|fail" log.txt

11. 匹配整个单词

grep -w "root" /etc/passwd            # 只匹配完整的单词 root
# 不会匹配 rooter 或 roots

12. 匹配整行

grep -x "exact match" file.txt        # 整行完全匹配

13. 使用 Perl 正则表达式(更强大)

grep -P "\d{3}-\d{2}-\d{4}" file.txt  # 匹配 SSN 格式
grep -P "(?<=@)[a-z]+\.com" emails.txt  # 使用后顾断言

14. 从文件读取模式

# patterns.txt 每行一个模式
grep -f patterns.txt file.txt

15. 使用多个 -e 参数

grep -e "error" -e "warning" -e "fail" log.txt

高级搜索

16. 只显示匹配的文件名

grep -l "TODO" *.py                  # 列出包含 TODO 的 Python 文件
grep -l -r "error" /var/log/         # 递归列出包含 error 的日志文件

17. 显示不匹配的文件名

grep -L "ERROR" *.log                # 列出不包含 ERROR 的日志文件

18. 限制匹配数量

grep -m 10 "error" log.txt           # 只显示前10个匹配

19. 显示字节偏移

grep -b "pattern" file.txt           # 显示匹配位置的字节偏移

20. 搜索时排除文件/目录

# 排除特定文件
grep -r "pattern" --exclude=*.log /var/

# 排除多个文件类型
grep -r "pattern" --exclude=*.{log,tmp,backup} /var/

# 排除特定目录
grep -r "pattern" --exclude-dir=node_modules /project/
grep -r "pattern" --exclude-dir={.git,node_modules,dist} /project/

# 从文件读取排除列表
grep -r "pattern" --exclude-from=exclude.txt /project/

21. 只搜索特定文件类型

grep -r "TODO" --include="*.py" --include="*.js" /project/
grep -r "error" --include="*.log" /var/log/

22. 处理二进制文件

grep -a "text" binary_file           # 强制当作文本处理
grep -I "pattern" binary_file        # 忽略二进制文件

实用组合命令

23. 搜索并高亮显示

grep --color=auto "error" log.txt    # 高亮匹配文本(默认 auto)
grep --color=always "error" log.txt  # 强制高亮(用于管道)

24. 管道组合

# 查看进程并过滤
ps aux | grep "nginx"

# 查看历史命令
history | grep "git"

# 查看日志并过滤
tail -f /var/log/syslog | grep "ERROR"

# 组合多个 grep
cat log.txt | grep "ERROR" | grep -v "IGNORE"
grep "ERROR" log.txt | grep -v "IGNORE"

# 统计匹配行数
grep "pattern" file.txt | wc -l

25. 查找文件内容并执行操作

# 查找包含 pattern 的文件并删除
grep -l "old_text" *.txt | xargs rm -f

# 查找并替换
grep -l "old" *.txt | xargs sed -i 's/old/new/g'

# 查找并统计
grep -r "pattern" . | wc -l

26. 显示前后文并高亮

grep -C 3 --color=auto "error" log.txt

27. 搜索日志文件中的 IP 访问次数

grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" access.log | sort | uniq -c | sort -nr

28. 查找密码文件中的用户

grep -E ":/bin/$" /etc/passwd    # 查找使用  的用户
grep -v "/nologin$" /etc/passwd      # 查找可登录的用户
grep -E "^(root|admin|test):" /etc/passwd  # 查找特定用户

29. 搜索 XML/HTML 标签内容

grep -oP '(?<=<title>)[^<]+' index.html   # 提取标题
grep -oP '(?<=href=")[^"]+' file.html      # 提取链接

30. 查找并统计错误类型

grep -o "ERROR: [A-Z]*" log.txt | sort | uniq -c

四、正则表达式快速参考

基本正则表达式(BRE - 默认)

元字符 说明 示例
. 匹配任意单个字符 a.c 匹配 abc, a1c
* 匹配前一个字符 0 次或多次 ab* 匹配 a, ab, abb
^ 匹配行首 ^Error 匹配行首的 Error
$ 匹配行尾 Done$ 匹配行尾的 Done
[] 字符类 [0-9] 匹配数字
[^] 否定字符类 [^0-9] 匹配非数字
\ 转义特殊字符 \. 匹配点号
\< 单词边界(起始) \<root
\> 单词边界(结束) root\>

扩展正则表达式(ERE - 使用 -E)

元字符 说明 示例
+ 前一个字符 1 次或多次 a+ 匹配 a, aa, aaa
? 前一个字符 0 次或 1 次 colou?r 匹配 color, colour
` ` 或(选其一)
() 分组 (abc)+ 匹配 abc, abcabc
{n} 恰好 n 次 [0-9]{5} 匹配 5 位数字
{n,} 至少 n 次 [0-9]{5,}
{n,m} n 到 m 次 [0-9]{2,4}

Perl 正则表达式(PCRE - 使用 -P)

元字符 说明 示例
\d 数字 \d+ 匹配数字
\w 单词字符 \w+ 匹配单词
\s 空白字符 \s+ 匹配空白
\b 单词边界 \bword\b
(?=) 正向先行断言 a(?=b) 匹配后跟 b 的 a
(?<=) 正向后顾断言 (?<=a)b 匹配前有 a 的 b
(?! ) 负向先行断言 a(?!b) 匹配不后跟 b 的 a
(?<!) 负向后顾断言 (?<!a)b 匹配前无 a 的 b

五、退出状态码

状态码 说明
0 找到匹配行
1 没有找到匹配行
2 发生错误(文件不存在、权限不足等)

在脚本中使用

if grep -q "pattern" file.txt; then
    echo "Pattern found"
else
    echo "Pattern not found"
fi

六、常用场景组合

1. 查找所有包含特定文本的文件

grep -r -l "search_text" /path/

2. 统计特定错误出现次数

grep -c "ERROR 404" access.log

3. 提取所有邮箱地址

grep -E -o "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" emails.txt

4. 查找并删除包含特定内容的行

sed -i '/pattern_to_delete/d' file.txt
# 或先查看再删除
grep -v "pattern_to_delete" file.txt > newfile.txt

5. 实时监控日志并过滤

tail -f /var/log/syslog | grep --color=auto "error\|warning"

6. 查看 CPU 使用率高的进程

ps aux | grep -v "grep" | grep "java"

7. 查找 404 错误并统计 URL

grep " 404 " access.log | grep -o "GET [^ ]*" | sort | uniq -c | sort -nr

8. 查找配置文件中非注释非空行

grep -v "^#" /etc/nginx/nginx.conf | grep -v "^$"

9. 比较两个文件的共同行

grep -f file1.txt file2.txt

10. 查找最近修改的文件中的内容

find . -type f -mtime -1 -exec grep -l "pattern" {} \;

11. 递归搜索并排除二进制文件

grep -r --binary-files=without-match "pattern" /path/

12. 搜索并显示行号,只显示文件名

grep -r -n "pattern" . | cut -d: -f1 | sort -u

七、性能优化技巧

1. 使用 -F 加速(固定字符串)

grep -F "static_string" large_file.txt   # 比正则快

2. 限制搜索范围

grep -m 10 "pattern" large_file.txt      # 找到10个就停止

3. 使用 LC_ALL=C 加速(C 语言环境)

LC_ALL=C grep "pattern" file.txt         # 处理 ASCII 时更快

4. 结合 find 使用

find /path -name "*.log" -exec grep "pattern" {} \;

5. 使用 ripgrep(rg)等替代工具(更快)

rg "pattern" /path/                      # 更快的替代品

八、常用别名设置

# 添加到 ~/.rc
alias grep='grep --color=auto'
alias egrep='grep -E --color=auto'
alias fgrep='grep -F --color=auto'
alias grepi='grep -i --color=auto'
alias grepr='grep -r --color=auto'

九、快速参考表

需求 命令
搜索字符串 grep "pattern" file
忽略大小写 grep -i "pattern" file
显示行号 grep -n "pattern" file
显示上下文 grep -C 3 "pattern" file
统计匹配行数 grep -c "pattern" file
只显示匹配部分 grep -o "pattern" file
显示不匹配的行 grep -v "pattern" file
递归搜索 grep -r "pattern" /path/
只显示文件名 grep -l "pattern" *
正则搜索 grep -E "pattern" file
搜索多个模式 grep -e "pat1" -e "pat2" file
高亮显示 grep --color "pattern" file
排除文件 grep --exclude=*.log "pattern" *
排除目录 grep --exclude-dir=dir "pattern" *

Attached Files

Loading attached files...
Search Results