55

Is it possible to pipe output (e.g. dmesg) to a command like less (or equivalent) and keep the text highlighting used by the original command?

example: on the left dmesg | less on the right dmesg

<code>dmesg | less</code> vs <code>dmesg</code>

Steven
  • 28,386
apoc
  • 763

3 Answers3

63

Use the --human parameter to view colored dmesg output in a less-like environment.

dmesg --human --color=always

Or use the short version:

dmesg -H -L=always

Alternatively, use the following command to achieve similar results.

dmesg --color=always | less -R

Many other utilities that produce colored output (ls, grep, etc.) have a similar --color=always option.

mustaqim
  • 3
  • 2
Steven
  • 28,386
4

A generic command to preserve coloration independent of the program providing the output is the unbuffer command (which is part of the expect package).

Usage:

unbuffer dmesg | less -R
1

Yes, it works nice.

  • for tree: tree -C | less -r

  • for ls: ls -lA --color=always | less -r

  • for pytest: py.test --color=yes | less -r

Peregrino69
  • 5,004