Forrest logo
back to the grep tool

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

This command is using the grep command-line tool for searching patterns in files.

Here's a breakdown of the options and arguments used:

  • grep: The command itself, used for searching text patterns.
  • --with-filename: This option specifies that the filename corresponding to each matched line should be displayed along with the matched line.
  • --line-number: This option causes grep to print the line number within a file before each line that matches the search pattern.
  • --color=always: This option enables colored highlighting of the matched patterns. The always keyword indicates that color highlighting should be used regardless of whether the output is being displayed on a terminal or redirected to a file.
  • "${search_pattern}": The search pattern enclosed in double-quotes. This represents the text or regular expression pattern that you want to search for.
  • ${filename}: The filename or path to the file(s) in which you want to search for the specified pattern.

When you run this command, it will search for the given pattern in the specified file(s) and display the matching lines along with the filename and line numbers. The matched patterns will be highlighted with colors.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the grep tool