Why does `less` treat a `.jpg` file as an image when Linux ignores extensions?
Asked by
MellowPine42
I'm trying to understand how Linux handles file extensions. I created a file with `touch banana.jpg`, then added plain text to it with `nano`. `cat banana.jpg` shows the text normally, but running `less banana.jpg` displays a message such as "No identify available" and suggests installing ImageMagick to browse images. Why is `less` treating the file as an image, and how can I make it display the file contents as ordinary text?
Answers
Linux itself generally doesnโt use the `.jpg` suffix to determine a fileโs type. However, many systems configure `less` to use a helper called `lesspipe`. The `LESSOPEN` environment variable points to that helper, which may inspect the filename extension and invoke programs for images, archives, compressed files, and other formats. This behavior depends on the distribution and its `less` configuration, so another system may display the text normally.
Reply to this answer
Check whether `LESSOPEN` is set with `env | grep LESSOPEN`. You can bypass the configured preprocessor for one command with `less -L banana.jpg`, or disable it in the current shell with `unset LESSOPEN`. After that, `less` should read the file directly and show the text, regardless of its `.jpg` name. The exact result can vary between distributions because not every system enables `lesspipe`.
Reply to this answer
Add your answer
Answers are reviewed before publication.
MellowPine42: That gives me a useful leadโ Iโll look into environment variables and how `LESSOPEN` is configured.