Let less
handle the problem, as it's good at it.
The pager less
is pretty good at formating binary files for output. Also, the pager features, like scrollback and searching are often useful.
It has no direct option for following a file that grows, but it has way to provide keystrokes to "type" after startup.
The key F starts following the growing file, updating the screen when the file grows.
Prefix the key with +
instead of -
and use that just like an option:
Replacing tail -f
:
less +F growing-file.log
To continue following the file by name, even if it's moved away or deleted, and recreated with the same name, add the option --follow-name
; It modifies what the key F and +F
do, it does not start following by itself:
Replacing tail -F
(--follow=name
):
less --follow-name +F rolling-file.log
If the file is recognized as a "binary file", less
asks whether you really want to see it - to avoid the question, use the option -f
(--force
) too:
less --follow-name +F -f rolling-binary-file.log
(Note that the meaning of -f
for less
is different from the same option for tail
.)
Use Ctrl+c to stop following, which allows you to scroll back or search.
- Start following again with F.
- Jump to start: g.
- Jump to start: G.
- Search: /.
- Quit: q.
Binary output example
$ less +F /usr/bin/vim
"/usr/bin/vim" may be a binary file. See it anyway?
After pressing y (which you can avoid with -f
) the screen shows:
^A^@^@^@^@^@<E5>
^A^@^@^@^@^@<E6>
^A^@^@^@^@^@^@^P^A^@^@^@^@^@^B^P^A^@^@^@^@^@8^P^A^@^@^@^@^@F^P^A^@^@^@^@^@
<F1>^A^@^@^@^@^@^P<F1>^A^@^@^@^@^@-<F1>^A^@^@^@^@^@0<F1>^A^@^@^@^@^@i<F1>^A
^L(^@^@^@^@^@^@^@^@^@^@^@^@^@<A3><A5>&^@^@^@^@^@^@^@^@^@^@^@^@^@b['^@^@^@^@
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@@<A7>&^@^@^@^@
<AB>&^@^@^@^@^@Ȣ&^@^@^@^@^@^L,^@^@^@^@^@^@<FF><FF><FF><FF><FF><FF><FF><FF>
^@^@^@^@^@^@s]&^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^_<D3>%^@^@^@^@^@̢
<AF>&^@^@^@^@^@<CA><(^@^@^@^@^@^B^D^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
<B1>&^@^@^@^@^@^D^D^H^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^R<B1>&^@^@
^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@<A9><B3>&^@^@^@^@^@D<B8>&^@^@
<B5>&^@^@^@^@^@^]T&^@^@^@^@^@^BL^@^@^@^@^@^@H<8A>O^@^@^@^@^@F@^@^@^@^@^@^@
K'^@^@^@^@^@^C<C4><FF><FF>^@^@^@^@^OK'^@^@^@^@^@^C<C3><FF><FF>^@^@^@^@^TK'
#^@^@^@^@^@^D^@^@^@^@^@^@^@<E7>v&^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
#^@^@^@^@^@^D^@^@^@^@^@^@^@<E7>v&^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
#^@^@^@^@^@p|#^@^@^@^@^@P~#^@^@^@^@^@^@^@^@^@^@^@^@^@^P
#^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@<90>{#^@^@^@^@^@^@^@^@^@^@^@^@^@
Waiting for data... (interrupt to abort)
Control chars are shown in carret notation like ^A
(^@
is the null byte).
They are also highlighted in the terminal (which is not shown above), to make the character Control-A, ^A
different from the character ^
followed by A
.
Configuration of how to show binary characters
From man less
:
Control and binary characters are displayed in standout (reverse
video). Each such character is displayed in caret notation if possible
(e.g. ^A for control-A). Caret notation is used only if inverting the
0100 bit results in a normal printable character. Otherwise, the char‐
acter is displayed as a hex number in angle brackets. This format can
be changed by setting the LESSBINFMT environment variable. LESSBINFMT
may begin with a "*" and one character to select the display attribute:
"*k" is blinking, "*d" is bold, "*u" is underlined, "*s" is standout,
and "*n" is normal. If LESSBINFMT does not begin with a "*", normal
attribute is assumed. The remainder of LESSBINFMT is a string which
may include one printf-style escape sequence (a % followed by x, X, o,
d, etc.). For example, if LESSBINFMT is "*u[%x]", binary characters
are displayed in underlined hexadecimal surrounded by brackets. The
default if no LESSBINFMT is specified is "*s<%02X>". Warning: the
result of expanding the character via LESSBINFMT must be less than 31
characters.
When the character set is utf-8, the LESSUTFBINFMT environment variable
acts similarly to LESSBINFMT but it applies to Unicode code points that
were successfully decoded but are unsuitable for display (e.g., unas‐
signed code points). Its default value is "<U+%04lX>". Note that
LESSUTFBINFMT and LESSBINFMT share their display attribute setting
("*x") so specifying one will affect both; LESSUTFBINFMT is read after
LESSBINFMT so its setting, if any, will have priority. Problematic
octets in a UTF-8 file (octets of a truncated sequence, octets of a
complete but non-shortest form sequence, illegal octets, and stray
trailing octets) are displayed individually using LESSBINFMT so as to
facilitate diagnostic of how the UTF-8 file is ill-formed.
less my-rolling-file.log
, when the file has been opened, press F (uppercase f) and you will have the same behavior as ` tail -f`. Don't know if will be better treating binary data. You have to try and let us know.less
command does not satisfy my as i have to make ctrl-c to be able to move in the history. For the binary, I got a popup asking me if i want to install new fonts for the terminal and it shows many times, the solution given with tail will be the best one for my case. Thanks anyway for your feedback.