Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I haven't done anything with Linux in a decade, so I hope this question is still relevant. I've always wanted to know how Linux did this, and I just now thought of it again.

In the Windows OS world, files are associated with an application by their extension. For example, foo.txt may be opened with notepad (txt). And foo.xls would be opened with Excel (xls).

With Linux, file names didn't have extensions (do they now?), so how did it know what app to use to open a file? If a file was called foo, and I double-clicked it, it would actually open within the correct application. How?

share|improve this question

migrated from stackoverflow.com Feb 23 '13 at 0:37

This question came from our site for professional and enthusiast programmers.

    
Try man 1 file. –  cnicutar Feb 22 '13 at 21:24
2  
Linux has no notion of file associations with apps. It's a feature of file managers, and each one may do it its own way. –  Barmar Feb 22 '13 at 21:44
add comment

2 Answers 2

up vote 3 down vote accepted

Actually, Linux itself doesn't care about file types, it only cares if a file is executable or not, and this is set by the properties of a file, not the content. However, the file tool, which is distributed with linux can help here. file compares the file contents against a database, and looks for a match. Various file formats use magic numbers in their headers, for instance, every JPEG file starts with FFD8, and ends with FFD9. you may remember that the old DOS executables always start with MZ, which is again a magic number.

share|improve this answer
add comment

Most Linux desktops use xdg-open for this behind the scenes. That queries the local xdg-mime XML database, which can identify files by their extensions, as on Windows, or by magic bytes in the files themselves, depending on configuration.

If you want to add support for additional file types, you can do so using xdg-mime. This page documents its XML semantics.

In principle it's also possible to use mailcap for this (with run-mailcap), which does only look at extensions, but I haven't seen a desktop environment that does that in a very long time.

share|improve this answer
add comment

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.