oneliner
Execute specific one-line commands
TLDR
Generate a shell command from plain English
Explain what a command does
Copy a generated command to the clipboard
Show a detailed, educational breakdown of a command
Execute a generated command (use with caution)
Interactively confirm before executing a generated command
SYNOPSIS
grep [OPTIONS] PATTERN [FILE...]
PARAMETERS
-i, --ignore-case
Ignore case distinctions in both the PATTERN and input files during the search.
-v, --invert-match
Select non-matching lines, effectively showing lines that do not contain the PATTERN.
-r, --recursive
Recursively search through directories, following symbolic links only if they are on the command line. Use `-R` for recursive search without following symlinks.
-n, --line-number
Prefix each line of output with the 1-based line number within its input file.
-c, --count
Suppress normal output; instead, print a count of matching lines for each input file.
-l, --files-with-matches
Suppress normal output; instead, print the name of each input file from which output would normally have been printed.
-q, --quiet, --silent
Suppress all normal output; exit immediately with zero status if any match is found, and with 1 if no match is found. Useful in scripts for conditional logic.
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE), which provides more powerful regex syntax compared to basic regular expressions (BREs).
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. This disables regular expression interpretation, making searches faster for literal strings.
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
-C NUM, -NUM, --context=NUM
Print NUM lines of context (NUM lines before and NUM lines after) around matching lines.
DESCRIPTION
The grep command (globally search a regular expression and print) is a fundamental utility in Unix-like operating systems, used for searching plain-text data sets for lines that match a regular expression. It's an indispensable tool for system administrators, developers, and anyone working with text files on the command line.
grep can search single files, multiple files, or the output of other commands via piping. Its power lies in its ability to use complex regular expressions to define search patterns, allowing for highly specific and flexible searches. It prints matching lines to standard output by default, making it easy to filter logs, find code snippets, or extract specific information from large text data. Its efficiency and versatility make it a cornerstone of shell scripting and data processing pipelines.
CAVEATS
- Performance: Searching very large files or an extremely large number of files recursively can be resource-intensive. For extremely large datasets, specialized tools or indexing might be more appropriate.
- Regular Expression Complexity: Overly complex regular expressions can be difficult to read, debug, and may also impact performance. Ensure patterns are as specific and efficient as possible.
- Binary Files: By default, grep might print "Binary file ... matches" or treat binary files differently. Use `--text` to force treating all files as text, or `--binary-files=without-match` to suppress output from binary files.
- Locale Issues: Regular expression matching behavior can sometimes be affected by locale settings (e.g., `LC_ALL`, `LC_COLLATE`), which might lead to unexpected results with character classes like `[a-z]`.
EXIT STATUS
grep's exit status is crucial for scripting:
- 0: One or more lines were selected (i.e., a match was found).
- 1: No lines were selected (i.e., no match was found).
- 2: An error occurred (e.g., syntax error in regex, inaccessible file).
REGULAR EXPRESSIONS (REGEX)
The core power of grep comes from its use of regular expressions. These are sequences of characters that define a search pattern.
- Basic Regular Expressions (BREs): The default `grep` mode. Some characters like `?`, `+`, `{}`, `()` need to be escaped with a backslash to be interpreted as special regex operators.
- Extended Regular Expressions (EREs): Enabled with `-E` or using `egrep`. These are generally more intuitive, as characters like `?`, `+`, `{}`, `()` are special without escaping.
- Fixed Strings: Enabled with `-F` or using `fgrep`. The pattern is treated as a literal string, not a regular expression, making it faster for exact string matches.
HISTORY
The grep command originated in Bell Labs Unix, developed by Ken Thompson in 1974. Its name is derived from a command in the `ed` text editor, where `g/re/p` meant "globally search for a regular expression and print".
Initially, there were separate commands like `egrep` (for extended regular expressions) and `fgrep` (for fixed strings). Over time, these functionalities were integrated into the single `grep` command, often controlled by options like `-E` and `-F`. The GNU `grep` implementation, which is standard on most Linux systems, has added numerous enhancements and features over the years, making it a highly robust and versatile tool.


