Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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 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)? –  Patrick May 14 at 4:39
    
What is LHS? It is new to me. –  dfc May 17 at 3:37
1  
@dfc left-hand side. –  Maciej Piechotka May 18 at 14:21

3 Answers 3

up vote 11 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

I can't comment yet, but when less claims the file is binary,

  less -r <file> 

will give you the expected result. (see FooF's answer)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.