They let the relevant reader know what kind of file it is. For a computer this might matter when “using” the file.
Let us say you want to display the file on a screen. You command the computer to open file *Dancing Dog.* How does a computer know what to do with the file if it doesn’t know what it is? Is it a text file that is a story? A video? A picture? The file extension is like a label that says “this one is a video.” Since the computer knows it is a video it will open the video player to display the file. If you use the wrong file extension by renaming the file to have a text document extension, it will try to open it on a text viewer. A text viewer probably is not designed to display video, so you’ll get an error of some sort.
They tell the OS what program is used to load that file.
The first sector of that file then confirms the type of file it is.
A jpg file says what dimensions the picture is, what size of pallet to use, where an excel file says how many worksheets are in the workbook, a mp3 file says what dynamic range is used, etc.
So a program tries to load that file, but if it has the wrong definitions (it’s a picture and not a URL), then the file fails to load.
So the file extension is just a helper to hAve the file loaded by an appropriate app vs having to figure it out each time.
It’s worth noting that the file extension is really just a part of the filename. The filename is just text, and by convention we consider the bit after the last period to be special.
But (aside from some OS-imposed restrictions) you can basically put whatever text you want in a filename. You can have files without extensions, files with made-up extensions, files with multiple extensions, etc. And renaming a file and changing the extension has no effect on the data itself.
It’s just that your OS has a list of extensions it knows and what it should do with files of that extension. When you try to open a file, the OS looks at the bit after the last period, says, “Do I know this type of file?” and if yes, it calls the appropriate program to open it. But that’s just something your OS does.
And yes, some types of files also have special data at the beginning that say what kind of data it is. This is why you can rename a jpg picture to png and most programs will still open it. Your OS recognizes that it’s an image file (even if you’ve named it the wrong type of image file) and calls a program that opens images. That program reads the data and recognizes the special data at the start that says, “Hey, I’m a jpg image” and so it opens the image correctly, ignoring the fact that you named it png.
They decide:
* what icon will be used for a file
* what text will appear in the “Type” column
* when you double-click a file – what action will be performed, which program will be started
* when you right-click – what options will appear on the list
Other than that – not much.
Your computer has a database of known extensions and all actions that can be performed on them. When you install a program that can open some type of file – it usually adds itself to the database.
The programs that actually open the file usually try to not depend on the extension – they usually search for “magic numbers” inside the file to determine the type. That’s why Paint can open a PNG file, even if you change the extension. However, some types have no magic numbers – so the program can still resort to using the extension.
Some programs can open any file, no matter the type. Notepad is one – it will try to decode the file as text, even if it isn’t one. Hex editors are specifically designed to view and modify the binary content of a file.
*Technically,* they don’t *do* anything. It’s just a part of a file name, and in some file systems there aren’t any extensions at all. I don’t know of a single FS that actually uses extensions on Linux, for example, and I’m fairly sure macOS doesn’t have them either. This, of course, doesn’t prevent these systems from using PDF files, for example, as they simply “know” that “that thing after the dot is maybe telling us what kind of file that is”. But they also heavily use files without extensions, for example most executable files on Linux, because Linux file systems has special file attributes to indicate that a file is exectuable.
On Windows, these are leftovers from DOS where a file name could only consist of a name between 1 and 8 (sic!) characters long and extension between 0 and 3 characters long, known as the 8.3 scheme. These restrictions have been lifted somewhere around Windows 95, but the idea of an extension remained, as it proved to be useful for indicating file types.
Therefore, we have a lot of file extensions now that are unofficially reserved for various file types. It’s not like they *must* be named like that, but it’s very convenient when they are, because it makes figuring out a lot of stuff about a file much easier. Any sensible OS has a set of properties registered for known file types (and any app can register their own types), and they determine a lot of useful data, such as:
1. What icon to display. Some files (like EXE) can actually have an icon inside, but in most cases it’s more useful to have an icon defined once and for all (like PDF).
2. What context actions are available. For example, when you right-click a PDF file, you can print it right away without opening it because your file manager knows a PDF can be printed.
3. What app (or apps) to open it with. This is configurable, and there may be multiple apps that match, in which case it’s useful to show a list of apps to choose from.
And there are many more applications for them. For example, a web server may know that if a file is named something.html, it should be sent to your browser as-is, but if it’s something.php it should be passed to the PHP interpreter to convert it to HTML first. Or when you’re browsing GitHub, it knows that files ending with MD are MarkDown files and therefore they’re nicely formatted for you instead of forcing you to look at raw MarkDown.
If a file doesn’t have an extension, figuring out what it is becomes a lot harder as, aside from a few specific cases, you can only guess by looking at its content. And it’s some binary garbage in many cases, so you need a hex editor/viewer to do that. An experienced user may guess some common types (like PDF files starting with “%PDF” or EXE files starting with MZ), and there is software that can do such guessing as well, like the `file` command on Linux, but it isn’t 100% reliable and much slower than just looking at the file name.
The part of a filename after the last dot is used to signify what type of file it is.
Most of it is to help humans be able to read things better, because programs can look at the content of a file to determine filetype.
having the type of the file in the name makes things easier, because this way you don’t even need to look at the content to know what type it is.
The OS can decide to try and open certain filenames with certain programs and built in functions of the OS.
Very old versions of DOS had no extensions at all and later ones only allowed 8 letters for the name of a file and 3 letters for the extension. The extensions created in those days were kept to this day and newer filenames created long after such limitations were lifted also often try to limit themselves to three letters.
Imagine you are a teacher and you enter a classroom full of kids who want to go to the bathroom. You know which bathrooms to direct them to (boys/girls), but don’t know which kid is which. So you ask them to identify themselves. When they say „boy“ you direct them to the boys washroom and when they say „girl“ you direct them to the girls washroom.
Extensions are like that, but for computers. Extensions is the kid identifying itself. Teacher is the computer.
It will tell the computer useful information – which helps the computer decide how to handle it (For example what program to open it with etc)
Latest Answers