grep commands

15 commands, each with a one-line explanation. Click a command to copy it, or its title for details.

Commands 15 shown

  • grep -ri 'error' /var/log/

    Search all files under /var/log for 'error' (any case)

  • grep -B3 -A5 'FATAL' app.log

    Print 3 lines before and 5 lines after each match

    debug

  • grep -c 'POST /api' /var/log/nginx/access.log

    Print only the number of matching lines

    monitoring

  • grep -oP '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' access.log | sort -u

    Extract IP addresses from log using Perl regex

  • grep -v '^#' /etc/nginx/nginx.conf | grep -v '^\s*$'

    Remove comment and blank lines from a config file

  • grep -r --include='*.py' 'import os' /srv/app/

    Limit recursive search to .py files only

  • grep -r --exclude-dir='.git' --exclude-dir='node_modules' 'TODO' .

    Search recursively skipping vcs and dependency dirs

  • grep -rn 'panic:' /var/log/app/

    Show file and line number for each match

    debug

  • rg 'TODO|FIXME|HACK' --type py -l

    List files containing TODO/FIXME/HACK in Python files

  • rg 'panic' -C3 --glob '*.go'

    3 lines of context around matches in Go files

    debug

  • rg 'secret_key' --hidden --no-ignore

    Search hidden and .gitignore'd files

    security

  • rg 'old_value' --replace 'new_value' --passthru config.yaml

    Preview how a replacement would look without writing

  • rg 'error' /var/log/ --stats 2>&1 | tail -5

    Show total matches, files searched, elapsed time

    monitoring

  • grep -a 'password' /proc/<pid>/mem 2>/dev/null | strings | head -20

    Treat binary as text (-a) to search process memory

    security · debug

  • grep -E '(ERROR|WARN|CRIT)' app.log | tail -50

    Match lines containing ERROR, WARN, or CRIT