Forrest logo
tool overview
On this page you find all important commands for the CLI tool grep. If the command you are looking for is missing please ask our AI.

grep

Grep searches one or more input files for lines containing a match to a specified pattern. By default, Grep outputs the matching lines.

Articles in our magazine for grep:

How to find files on the command line?

Sometimes it is important to find certain files. Be it particularly large ones or ones that contain certain information. All this is just a command away on the command line.

List of commands for grep:

  • file_manipulation:warp:3dcc9 Recursively find and replace within a directory
    $ grep -rl ${old_text} ${file_path} | xargs sed -i '' 's/${old_text}/${new_text}/g'
    try on your machine
    explain this command
  • file_manipulation:warp:b258ada17cb9a5d07cfcab2cde27931b Find all files in a directory that don't contain a string
    $ grep -L "${pattern}"
    try on your machine
    explain this command
  • file_manipulation:warp:bf4fd Recursively search through files that match an extension
    $ grep -r --include=\*.${extension} '${search_term}' ${file_name}
    try on your machine
    explain this command
  • files:find:pattern Find all files with a specific text pattern in it.
    $ grep -rnw '${path_to_check}' -e '${pattern}'
    try on your machine
  • grep:tldr:1682e grep: Search for a pattern within a file.
    $ grep "${search_pattern}" ${filename}
    try on your machine
    explain this command
  • grep:tldr:274a3 grep: Search for a pattern in all files recursively in a directory, showing line numbers of matches, ignoring binary files.
    $ grep --recursive --line-number --binary-files=${without-match} "${search_pattern}" ${path-to-directory}
    try on your machine
    explain this command
  • grep:tldr:5c5ea grep: Print file name and line number for each match with color output.
    $ grep --with-filename --line-number --color=always "${search_pattern}" ${filename}
    try on your machine
    explain this command
  • grep:tldr:644a3 grep: Search for an exact string (disables regular expressions).
    $ grep --fixed-strings "${exact_string}" ${filename}
    try on your machine
    explain this command
  • grep:tldr:9fdd1 grep: Print 3 lines of context around, before, or after each match.
    $ grep --${select}=${3} "${search_pattern}" ${filename}
    try on your machine
    explain this command
  • grep:tldr:e135a grep: Search for lines matching a pattern, printing only the matched text.
    $ grep --only-matching "${search_pattern}" ${filename}
    try on your machine
    explain this command
  • grep:tldr:e697e grep: Use extended regular expressions (supports `?`, `+`, `{}`, `()` and `|`), in case-insensitive mode.
    $ grep --extended-regexp --ignore-case "${search_pattern}" ${filename}
    try on your machine
    explain this command
  • sponge:tldr:fadef sponge: Remove all lines starting with # in a file.
    $ grep -v '^${#}' ${filename} | sponge ${filename}
    try on your machine
    explain this command
tool overview