Linux associate file name extension with opening program
Overview
Manually associate file extensions and open programs for Linux.
When Linux does not recognize a certain extension, we need to create a MIME type for the extension. Linux decides opening program by MIME type, not directly file name extension.
Then we associate this MIME type with a program menu item.
Manual associate steps
-
Create a MIME type for the file name extension
If your Linux already recognizes this extension, you don't need to do this step. In your file manager, check to see if the file with this extension is correctly recognized. Or use
file
Command to view.Create
application-x-SUFFIX.xml
Content:1<?xml version="1.0" encoding="UTF-8"?> 2<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"> 3 <mime-type type="application/x-SUFFIX"> 4 <comment>YOUR DESCRIPTION FOR THIS SUFFIX</comment> 5 <icon name="application-x-SUFFIX"/> 6 <glob-deleteall/> 7 <glob pattern="*.SUFFIX"/> 8 </mime-type> 9</mime-info>
Put
application-x-SUFFIX.xml
To~/.local/share/mime/packages/
。
Refresh the system MIME database1$ update-mime-database ~/.local/share/mime
-
Build a menu item for the program
If this program already has a menu item, skip this step.
Create
PROGRAM.desktop
, Content:1[Desktop Entry] 2Version=1.0 3Type=Application 4Name=PROGRAM NAME TO DISPLAY 5Icon="/path-to/ICON FILE" 6Exec="/path-to/EXECUTABLE" %f 7Path="/path-to/" (optional) 8NoDisplay=false (true=not show in system menu) 9Categories=Utility; 10StartupNotify=false 11Terminal=false
The key is, on line
Exec=
,%f
passes the file path to the executing program.
ForExec=
you can also use the command directly without an absolute path. ForIcon=
you can also directly write the icon name recognized by the system without using a path.
PutPROGRAM.desktop
to~/.local/share/applications/
-
Set a program to open MIME type files
Modify~/.config/mimeapps.list
or~/.local/share/applications/mimeapps.list
(abandoned)1[Added Associations] 2application/x-SUFFIX=PROGRAM.desktop;OTHER-PROGRAM.desktop;OTHER-PROGRAM2.desktop;
among them
application/x-SUFFIX
Is the MIME type you just created. If the MIME type is the original one and not just created, replace it with the original MIME type name.
Reference
https://wiki.archlinux.org/index.php/Default_applications#MIME_database
Buy me a coffee
Your donation will be an impetus for me!