{"tool":"grep","name":"grep","description":"Searches text for lines matching a pattern, with regular expressions, recursion and context.","tags":["search","regex","text"],"homepage":"https://www.gnu.org/software/grep/","docs":"https://man7.org/linux/man-pages/man1/grep.1.html","install":{"apt":"sudo apt install grep","brew":"brew install grep"},"related":["sed","awk","find"],"total_commands":21,"commands":[{"id":11,"category":"Basics","command":"grep \"<pattern>\" <file>","example":"grep \"ERROR\" app.log","explanation":"Prints every line containing the pattern."},{"id":12,"category":"Basics","command":"grep -i \"<pattern>\" <file>","example":"grep -i \"error\" app.log","explanation":"Case-insensitive match."},{"id":13,"category":"Basics","command":"grep -v \"<pattern>\" <file>","example":"grep -v \"DEBUG\" app.log","explanation":"Inverts the match: prints lines that do not contain the pattern."},{"id":14,"category":"Basics","command":"grep -n \"<pattern>\" <file>","example":"grep -n \"TODO\" main.c","explanation":"Prefixes each match with its line number."},{"id":15,"category":"Basics","command":"grep -c \"<pattern>\" <file>","example":"grep -c \"Failed password\" /var/log/auth.log","explanation":"Counts matching lines instead of printing them."},{"id":16,"category":"Basics","command":"grep -w \"<word>\" <file>","example":"grep -w \"root\" /etc/passwd","explanation":"Matches whole words only, so “root” does not match “chroot”."},{"id":17,"category":"Basics","command":"grep -x \"<line>\" <file>","example":"grep -x \"PermitRootLogin yes\" /etc/ssh/sshd_config","explanation":"Matches only if the entire line is the pattern."},{"id":18,"category":"Searching trees","command":"grep -r \"<pattern>\" <dir>","example":"grep -r \"password\" /etc","explanation":"Searches every file under a directory recursively."},{"id":19,"category":"Searching trees","command":"grep -rl \"<pattern>\" <dir>","example":"grep -rl \"api_key\" ~/projects","explanation":"Lists only the names of files that match."},{"id":20,"category":"Searching trees","command":"grep -rn --include=\"<glob>\" \"<pattern>\" <dir>","example":"grep -rn --include=\"*.php\" \"eval(\" /var/www","explanation":"Restricts the search to certain file types."},{"id":21,"category":"Searching trees","command":"grep -r --exclude-dir=<dir> \"<pattern>\" .","example":"grep -r --exclude-dir=node_modules \"TODO\" .","explanation":"Skips directories you do not care about."},{"id":22,"category":"Searching trees","command":"grep -rIl \"<pattern>\" <dir>","example":"grep -rIl \"BEGIN RSA PRIVATE KEY\" / 2>/dev/null","explanation":"Skips binary files (-I) — here hunting for private keys left on disk."},{"id":23,"category":"Patterns","command":"grep -E \"<a>|<b>\" <file>","example":"grep -E \"error|fail|denied\" syslog","explanation":"Extended regular expressions: alternation, +, ? and {n} without backslashes."},{"id":24,"category":"Patterns","command":"grep -o \"<regex>\" <file>","example":"grep -oE \"[0-9]{1,3}(\\.[0-9]{1,3}){3}\" access.log","explanation":"Prints only the matched part of each line — here, every IPv4 address."},{"id":25,"category":"Patterns","command":"grep -F \"<string>\" <file>","example":"grep -F \"a.b*c\" notes.txt","explanation":"Treats the pattern as a fixed string, not a regex — faster and no escaping."},{"id":26,"category":"Patterns","command":"grep -P \"<regex>\" <file>","example":"grep -P \"\\d{4}-\\d{2}-\\d{2}\" app.log","explanation":"Perl-compatible regex with \\d, lookarounds and lazy quantifiers (GNU grep)."},{"id":27,"category":"Patterns","command":"grep -f <patterns> <file>","example":"grep -f iocs.txt proxy.log","explanation":"Reads many patterns from a file, one per line — handy for IOC matching."},{"id":28,"category":"Context","command":"grep -A <n> \"<pattern>\" <file>","example":"grep -A 3 \"Exception\" app.log","explanation":"Shows n lines after each match."},{"id":29,"category":"Context","command":"grep -B <n> \"<pattern>\" <file>","example":"grep -B 2 \"segfault\" kern.log","explanation":"Shows n lines before each match."},{"id":30,"category":"Context","command":"grep -C <n> \"<pattern>\" <file>","example":"grep -C 5 \"Accepted password\" auth.log","explanation":"Shows n lines either side."},{"id":31,"category":"Context","command":"<command> | grep \"<pattern>\"","example":"ps aux | grep sshd","explanation":"Filters the output of another command."}],"slug":"grep","category":"linux","url":"https://terminal.rokibulroni.com/tool/linux/grep","markdown":"https://terminal.rokibulroni.com/api/v1/tools/linux/grep.md"}