grep --color

grep --color
Image credit: Unknown. Please contact us if you are the owner.

The grep --color command is used to highlight the matching parts of the text that the grep command finds.
This makes it easier to visually identify the search results.

🎧


Here's the overview:

$ grep -i --color 'failed' /var/log/auth.log

This command searches for the word 'failed' in /var/log/auth.log and highlights the matched text in red, which is the default color.

Sep 26 05:49:51 SNUBmonkey sshd[2275734]: Failed password for invalid user ubnt from 116.132.42.170 port 43430 ssh2
Sep 26 05:49:58 SNUBmonkey sshd[2275824]: Failed password for invalid user ubnt from 36.138.132.109 port 52319 ssh2
Sep 26 05:52:02 SNUBmonkey sshd[2277433]: Failed password for invalid user ubnt from 90.137.97.230 port 34675 ssh2
Sep 26 05:52:12 SNUBmonkey sshd[2277548]: Failed password for invalid user ubnt from 122.140.193.23 port 38870 ssh2
Sep 26 05:56:15 SNUBmonkey sshd[2280678]: Failed password for invalid user config from 79.129.206.144 port 58082 ssh2
Sep 26 06:01:19 SNUBmonkey sshd[2284728]: Failed password for invalid user user from 103.35.169.154 port 45587 ssh2


Additional Options:

• --color=always: Always output color, even when piping or redirecting the output.
• --color=auto: Only use color when the output is to a terminal (default).
• --color=never: Never output color.


Change Color


By default, matches highlighted with the grep --color option appear in red.
To change the color of the highlighted matches in grep , you can use the GREP_COLORS environment variable. This variable allows you to customize how grep highlights matches, separators, and line numbers.

$ GREP_COLORS='mt=01;32' grep -i --color=always 'failed' /var/log/auth.log

In this example, 01;32 specifies bold green text for the matching pattern.

Sep 26 05:49:51 SNUBmonkey sshd[2275734]: Failed password for invalid user ubnt from 116.132.42.170 port 43430 ssh2
Sep 26 05:49:58 SNUBmonkey sshd[2275824]: Failed password for invalid user ubnt from 36.138.132.109 port 52319 ssh2
Sep 26 05:52:02 SNUBmonkey sshd[2277433]: Failed password for invalid user ubnt from 90.137.97.230 port 34675 ssh2
Sep 26 05:52:12 SNUBmonkey sshd[2277548]: Failed password for invalid user ubnt from 122.140.193.23 port 38870 ssh2
Sep 26 05:56:15 SNUBmonkey sshd[2280678]: Failed password for invalid user config from 79.129.206.144 port 58082 ssh2
Sep 26 06:01:19 SNUBmonkey sshd[2284728]: Failed password for invalid user user from 103.35.169.154 port 45587 ssh2

Breakdown of the GREP_COLORS Syntax:

mt stands for matched text.
01: Bold.
32: Green.

Common Color Codes:

Black: 30
Red: 31
Green: 32
Yellow: 33
Blue: 34
Magenta: 35
Cyan: 36
White: 37


Set it globally (optional)

You can also add the following line to your shell configuration file (~/.bashrc or ~/.zshrc) to always use the desired color as such:

export GREP_COLORS='mt=01;31'




Well, that's a wrap on how to make grep --color your colorful friend for highlighting matches! Keep grepping and stay colorful!

We hope you found these insights useful!

Keep Us Caffeinated  ⦿ ⦿