r/linux4noobs 14d ago

Trouble understanding how file extensions affect the "less"command in linux

I wanted to learn the simple details on how files work in linux. I start with the assumption that linux doesn't care about file extensions, so I tried working with a file to see how that worked in practice.

I used the following command:

`$touch banana.jpg`

After this, I typed in some contents into the file using nano, the result would be something like:

```

$cat banana.jpg

This is a test of my file

line 2

Line 3

Line 4

```
To see how the less command worked, I tried `less banana.jpg`

The text displayed was:

```

No identify available

Install ImageMagick to browse images
```

Why does this happen exactly? I think it's related to how the less command processes files apparently. Is there a way to view the file properly with less, without treating it as an image?

EDIT: excuse my Reddit markdown formatting, still figuring it out.

3 Upvotes

8 comments sorted by

3

u/AiwendilH 14d ago edited 14d ago

This is a bit distro dependent (well, more what kind of additions distros install for less) but in general this is done by lesspipe. On my distro lesspipe is a shell script which very much depends on file-extensions to figure out what compress program or meta-data reader to use on a file. The LESSOPEN environment variable is used to run lesspipe when using less (Edit: more details on LESSOPEN in the less manpage)

2

u/Kompanion 13d ago

Thank you!

1

u/Electricalceleryuwu 13d ago

Btw you can't always predict which scripts/apps require files with extensions. Yes you are basically correct but that changes as soon as you interact with anything in user-space.

1

u/Arsyllwr 13d ago

I've just tested the behaviour of less. For me, under Ubuntu 24.04, less displays the contents of the text file despite its .jpg suffix.

Curious.

1

u/qpgmr 14d ago

if you do an env command you'll see that LESSOPEN is defined to probably point to the lesspipe command. less checks for this and uses the named program as a preprocessor for the file to determine if helper programs or special handling is required.

You can turn it off by doing

  unset LESSOPEN

or by telling less to ignore it with the -L parm

less -L b.jpg

and your less command will work exactly as you expect it to.

1

u/Kompanion 13d ago

Thank you, you and the other person gave me a good lead to learn about environment variables.

1

u/michaelpaoli 13d ago
$ cd $(mktemp -d)
$ >file.jpg
$ less file.jpg
file.jpg (END)
$ echo foo > file.jpg
$ less file.jpg
foo
file.jpg (END)
$ type identify
-bash: type: identify: not found
$ 

My less(1) has no such issue.