An extension is only part of a name. When something called photo.jpg won’t open, I ask file what is inside it.
file photo.jpg data.bin
It looks at the bytes and answers per file. A program renamed to look like a picture is reported as what it is, something like ELF 64-bit LSB pie executable, and a mystery data.bin may turn out to be plain ASCII text. You’ll also meet answers for archives, images with their dimensions, and scripts.
For scripts, the MIME type is easier to compare than the description:
file --mime-type -b photo.jpg
-b leaves out the file name, so you get just the type. Wildcards work too, which is a quick way to survey a folder of downloads without extensions:
file ~/Downloads/*
I find it especially handy before piping something unknown into a tool that expects text, or when a “zip” download turns out to be an HTML error page.
It’s an educated guess based on known patterns. A damaged file can keep a valid header and still be reported as fine, and unusual formats often come back as plain data. It tells you nothing about whether a file is safe to open. Some minimal systems and containers don’t include it.
Further reading: file(1) man page