20

Sometimes less wrongly recognize file as binary and tries to show hexdump on LHS (usually ones with non-alphanumeric characters but still containing printable ASCII characters). How to force it to recognize it as text?

  • Looking for another answer. I have a log file that cat somefile shows as ASCII, but less somefile says may be binary and displays as such. echo $LESSOPEN is empty and less -L doesn't help. – Nathan Long May 13 '14 at 15:09
  • 2
    @NathanLong This would probably be acceptable to open a new question on since the solution here does not solve your problem (just make sure to note that). However when you say "may be binary and displays as such", are you saying it display a hex representation of the data, or you see the raw binary data (as all the weird symbols and such)? – phemmer May 14 '14 at 04:39
  • What is LHS? It is new to me. – dfc May 17 '14 at 03:37
  • 1

2 Answers2

26

I think you have (or your distribution has) a LESSOPEN filter set up for less. Try the following to tell less to not use the filter:

less -L my_binary_file

For further exploration, also try echo $LESSOPEN. It probably contains the name of a shell script (/usr/bin/lesspipe for me), which you can read through to see what sort of filters there are. Also try man less, and read the Input Preprocessor section.

Jander
  • 16,682
  • I was using less into a script of a service and it wont work until i use /usr/bin/lesspipe – Juan Oct 04 '22 at 17:07
4

For the case of LESSOPEN not being the issue. The following less options could be tried:

  1. -f or --force: Besides allowing non-regular files to be opened, it "also suppresses the warning message when a binary file is opened".
  2. -r or --raw-control-chars: "Causes raw control characters to be displayed. The default is to display control characters using the caret notation; for example, a control-A (octal 001) is displayed as ^A. Warning: when the -r option is used, less cannot keep track of the actual appearance of the screen (since this depends on how the screen responds to each type of control character). Thus, various display problems may result, such as long lines being split in the wrong place.
  3. -R or --RAW-CONTROL-CHARS: Weaker form of -r above, only ANSI "color" escape sequences are output in "raw" form. "Unlike -r, the screen appearance is maintained correctly in most cases." This is useful if you are for example viewing a log file that uses ANSI color control sequences.

You can experiment by pressing keys - followed by r without rerunning less. You can add these options to LESS environment variable to make the setting permanent (e.g. by modifying your shell profile file); if your terminal can show colors I highly recommend adding -R in your LESS setting. You should make sure your terminal is configured to use the same character encoding as your text file.

FooF
  • 655