<p>I'm developing a Linux application that has its own file format. I want my app to open when you double-click on those files.</p>

<p>How can I register a file extension and associate it with my application on Linux? I'm looking for a way that is standard (works with GNOME and KDE based systems) and can be done automatic when my program is installed or run for the first time.</p>

[{'answer_id': 30939, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': "<p>1) in linux this is a function of your desktop environment rather than the os itself.<br>\n2) GNOME and KDE have different methods to accomplish this.<br>\n3) There's nothing stopping you from doing it both ways.</p>\n"}, {'answer_id': 31827, 'author': 'Kai', 'author_id': 2963, 'author_profile': 'https://Stackoverflow.com/users/2963', 'pm_score': 3, 'selected': False, 'text': '<p>There are two parts to this. You need to <a href="http://www.freedesktop.org/wiki/Specifications/AddingMIMETutor" rel="noreferrer">register a new file type</a> and then <a href="http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html" rel="noreferrer">create a desktop entry for your application</a>. The desktop entry associates your application with your new mime type.</p>\n\n<p>I thought that both Gnome and KDE (maybe only 4+?) used the <a href="http://www.freedesktop.org/wiki/Specifications/shared-mime-info-spec" rel="noreferrer">freedesktop shared mime info spec</a>, but I may well be wrong.</p>\n'}, {'answer_id': 31836, 'author': 'skolima', 'author_id': 3205, 'author_profile': 'https://Stackoverflow.com/users/3205', 'pm_score': 7, 'selected': True, 'text': '<p>Use <code>xdg-utils</code> from <a href="http://portland.freedesktop.org/wiki/" rel="noreferrer">freedesktop.org Portland</a>.</p>\n<p>Register the icon for the MIME type:</p>\n<pre class="lang-sh prettyprint-override"><code>xdg-icon-resource install --context mimetypes --size 48 myicon-file-type.png x-application-mytype\n</code></pre>\n<p>Create a configuration file (<a href="http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-latest.html" rel="noreferrer">freedesktop Shared MIME documentation</a>):</p>\n<pre><code>&lt;?xml version=&quot;1.0&quot;?&gt;\n&lt;mime-info xmlns=\'http://www.freedesktop.org/standards/shared-mime-info\'&gt;\n  &lt;mime-type type=&quot;application/x-mytype&quot;&gt;\n    &lt;comment&gt;A witty comment&lt;/comment&gt;\n    &lt;comment xml:lang=&quot;it&quot;&gt;Uno Commento&lt;/comment&gt;\n    &lt;glob pattern=&quot;*.myapp&quot;/&gt;\n  &lt;/mime-type&gt;\n&lt;/mime-info&gt;\n</code></pre>\n<p>Install the configuration file:</p>\n<pre class="lang-sh prettyprint-override"><code>xdg-mime install mytype-mime.xml\n</code></pre>\n<p>This gets your files recognized and associated with an icon. <a href="http://portland.freedesktop.org/xdg-utils-1.0/xdg-mime.html" rel="noreferrer"><code>xdg-mime default</code></a> can be used for associating an application with the MIME type after you get a <a href="http://portland.freedesktop.org/xdg-utils-1.0/xdg-desktop-menu.html" rel="noreferrer"><code>.desktop</code></a> file installed.</p>\n'}, {'answer_id': 21085981, 'author': 'fastrizwaan', 'author_id': 3189318, 'author_profile': 'https://Stackoverflow.com/users/3189318', 'pm_score': 2, 'selected': False, 'text': '<p>Try this script: needs:</p>\n\n<pre><code>1. your application icon -&gt; $APP               = FIREFOX.png \n2. your mimetype icon    -&gt; application-x-$APP = HTML.png\n</code></pre>\n\n<p>in the current directory:</p>\n\n<hr>\n\n<pre><code>#BASH SCRIPT: Register_my_new_app_and_its_extension.sh\nAPP="FOO"\nEXT="BAR"\nCOMMENT="$APP\'s data file"\n\n# Create directories if missing\nmkdir -p ~/.local/share/mime/packages\nmkdir -p ~/.local/share/applications\n\n# Create mime xml \necho "&lt;?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?&gt;\n&lt;mime-info xmlns=\\"http://www.freedesktop.org/standards/shared-mime-info\\"&gt;\n    &lt;mime-type type=\\"application/x-$APP\\"&gt;\n        &lt;comment&gt;$COMMENT&lt;/comment&gt;\n        &lt;icon name=\\"application-x-$APP\\"/&gt;\n        &lt;glob pattern=\\"*.$EXT\\"/&gt;\n    &lt;/mime-type&gt;\n&lt;/mime-info&gt;" &gt; ~/.local/share/mime/packages/application-x-$APP.xml\n\n# Create application desktop\necho "[Desktop Entry]\nName=$APP\nExec=/usr/bin/$APP %U\nMimeType=application/x-$APP\nIcon=$APP\nTerminal=false\nType=Application\nCategories=\nComment=\n"&gt; ~/.local/share/applications/$APP.desktop\n\n# update databases for both application and mime\nupdate-desktop-database ~/.local/share/applications\nupdate-mime-database    ~/.local/share/mime\n\n# copy associated icons to pixmaps\ncp $APP.png                ~/.local/share/pixmaps\ncp application-x-$APP.png  ~/.local/share/pixmaps\n</code></pre>\n\n<p>make sure:\nFOO binary is there in /usr/bin (or in $PATH)</p>\n'}, {'answer_id': 73411313, 'author': 'phil294', 'author_id': 3779853, 'author_profile': 'https://Stackoverflow.com/users/3779853', 'pm_score': 0, 'selected': False, 'text': '<p>This is all existing answers combined, completed and corrected into a single bash script.</p>\n<pre class="lang-bash prettyprint-override"><code>#!/bin/bash\nset -e # stop on error\n\nAPP=my-app\nEXT=my-app\nCOMMENT=Comment\nEXEC=/usr/bin/my-app\nLOGO=./logo.png\n\nxdg-icon-resource install --context mimetypes --size 48 $LOGO application-x-$APP\n\necho &quot;&lt;?xml version=\\&quot;1.0\\&quot; encoding=\\&quot;UTF-8\\&quot;?&gt;\n&lt;mime-info xmlns=\\&quot;http://www.freedesktop.org/standards/shared-mime-info\\&quot;&gt;\n    &lt;mime-type type=\\&quot;application/x-$APP\\&quot;&gt;\n        &lt;comment&gt;$COMMENT&lt;/comment&gt;\n        &lt;icon name=\\&quot;application-x-$APP\\&quot;/&gt;\n        &lt;glob pattern=\\&quot;*.$EXT\\&quot;/&gt;\n    &lt;/mime-type&gt;\n&lt;/mime-info&gt;&quot; &gt; $APP-mime.xml\n\nxdg-mime install $APP-mime.xml\nrm $APP-mime.xml\nupdate-mime-database $HOME/.local/share/mime\n\necho &quot;[Desktop Entry]\nName=$APP\nExec=$EXEC %U\nMimeType=application/x-$APP\nIcon=application-x-$APP\nTerminal=false\nType=Application\nCategories=\nComment=$COMMENT\n&quot;&gt; $APP.desktop\ndesktop-file-install --dir=$HOME/.local/share/applications $APP.desktop\nrm $APP.desktop\nupdate-desktop-database $HOME/.local/share/applications\n\nxdg-mime default $APP.desktop application/x-$APP\n\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/30931', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3306/']