Online Linux Terminal Logo
OnlineLinuxTerminal
文本处理CLI Manual

$grep

使用正则表达式或匹配字符串在文本文件与输出流中搜索内容。

命令语法POSIX Standard
grep [OPTIONS] PATTERN [FILE...]
Linux 社区2.4k 在线

与真实 Linux 用户交流,学习常用命令并免费获取精美速查表 PDF。

在交互式终端沙盒中实时运行

无需安装的客户端实时仿真环境。

在全屏终端工作区中打开 ↗
bash — terminal

概述与工作原理

Grep 是 Linux/Unix 环境下最著名的文本过滤与正则检索工具,逐行扫描输入内容并输出符合条件的行。

常用参数与选项说明

Flag / OptionDescription
-iIgnore case distinctions in both patterns and input data.
-nPrefix each matched output line with its 1-based line number.
-vInvert the match to print lines that do NOT match the pattern.
-r, -RRecursively search all files under each directory.
-cSuppress normal output and print only the count of matching lines.
-EInterpret pattern as an Extended Regular Expression (ERE).

实际生产案例与代码范例

#1Search for errors in system log files
grep -i -n "error" /var/log/syslog

Searches /var/log/syslog for case-insensitive occurrences of "error" and prints line numbers.

#2Filter process list through a pipe
ps aux | grep nginx

Pipes process snapshot data and isolates lines containing the nginx web server process.

#3Count total failed authentication attempts
grep -c "Failed password" /var/log/auth.log

Outputs the integer count of failed login attempts without listing every individual line.