This question is for a school project so i'm not looking to just be given a block of code or anything, just some direction on how to go about my work.

I want to be able to have my own custom file extension recognized within the OS(for example textfile.hello) and be able to do things such as making all .hello file open by default with some text editor.

How would I go about achieving this behavior? From my understanding I will have to write some code that will be operate within the file subsystem of the kernel, but not 100% certain how to go about it or if that is even correct.

Thanks in advance!

  • 3
    This behavior is going to be dictated by your display manager. Here for example is instructions for KDE: unix.stackexchange.com/questions/41195/… – Stephen Rauch Mar 12 '17 at 0:47
  • 2
    @StephenRauch File manager, not display manager. More generally, GUI toolkit. The display manager is the GUI program that prompts for a password and starts the user's session. – Gilles Mar 12 '17 at 23:37

As Stephen stated in the comments, recognizing files by extension and associating them with a program to open them would generally be done by your desktop environment (KDE, gnome, etc). On the other hand, in many cases, DEs (Desktop Environments) will use the utility file (or similar utility) to determine the type using the file's magic number. If that's the case, you might have to define a more specific rule. For example, on my computer - which uses Kubuntu, I use Kate to open plain ascii and utf files, but when said files end with .c, .h, .cpp, .hpp, .cmake, etc, they are opened with emacs instead. As @quixotic pointed out, this matching of extensions is generally done with xdg-open.

In most DEs, you can change this setting somewhere in the system settings menu, or by manually defining it during a [ right-click -> open with ] operation.

As a side note (not really on topic, but related) there are also ways to recognize some specialized ascii files even without extensions: as an example, a properly made shell script will contain a shebang before any non-remark line.

  • mime-types are used more than file in the modern DEs, i think. xdg-open and all that. – quixotic Mar 24 '17 at 22:47
  • @quixotic I'd imagine they use a combination of both (or something remarkably similar) as a file named myfile could be an ELF, a shell script, or a normal ASCII file, but KDE manages to determine the difference without any extension – Schives Mar 24 '17 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.