Red Hat Training
A Red Hat training course is available for Red Hat Enterprise Linux
12.3. Configuring File Associations
12.3.1. What Are MIME Types?
In GNOME, MIME (Multipurpose Internet Mail Extension) types are used to identify the format of a file. The GNOME Desktop uses MIME types to:
- Determine which application should open a specific file format by default.
- Register other applications that can also open a specific file format.
- Provide a string describing the type of a file, for example, in a file properties dialog of the Files application.
- Provide an icon representing a specific file format, for example, in a file properties dialog of the Files application.
MIME type names follow a given format:
media-type/subtype-identifier
Example 12.7. MIME Types Format
image/jpeg
is an example of a MIME type where image
is the media type, and jpeg
is the subtype identifier.
GNOME follows the freedesktop.org Shared MIME Info specification to determine:
- The machine-wide and user-specific location to store all MIME type specification files.
- How to register a MIME type so that the desktop environment knows which applications can be used to open a specific file format.
- How the user can change which applications should open what file formats.
12.3.1.1. What Is the MIME Database?
The MIME database is a collection of all MIME type specification files that GNOME uses to store information about known MIME types.
The most important part of the MIME database from the system administrator's point of view is the
/usr/share/mime/packages/
directory where the MIME type related files specifying information on known MIME types are stored. One example of such a file is /usr/share/mime/packages/freedesktop.org.xml
, specifying information about the standard MIME types available on the system by default. That file is provided by the shared-mime-info package.
Getting More Information
For detailed information describing the MIME type system, see the freedesktop.org Shared MIME Info specification located at the freedesktop.org website:
12.3.2. Adding a Custom MIME Type for All Users
To add a custom MIME type for all users on the system and register a default application for that MIME type, you need to create a new MIME type specification file in the
/usr/share/mime/packages/
directory and a .desktop
file in the /usr/share/applications/
directory.
Procedure 12.3. Adding a Custom application/x-newtype
MIME Type for All Users
- Create the
/usr/share/mime/packages/application-x-newtype.xml
file:<?xml version="1.0" encoding="UTF-8"?> <mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"> <mime-type type="application/x-newtype"> <comment>new mime type</comment> <glob pattern="*.xyz"/> </mime-type> </mime-info>
The sampleapplication-x-newtype.xml
file above defines a new MIME typeapplication/x-newtype
and assigns file names with the.xyz
extension to that MIME type. - Create a new
.desktop
file named, for example,myapplication1.desktop
, and place it in the/usr/share/applications/
directory:[Desktop Entry] Type=Application MimeType=application/x-newtype Name=My Application 1 Exec=myapplication1 field_code
The samplemyapplication1.desktop
file above associates theapplication/x-newtype
MIME type with an application named My Application 1, which is run by the commandmyapplication1
.Based on howmyapplication1
gets started, choose one respective field code from Desktop Entry Specification. For example, for an application capable of opening multiple files, use:Exec=myapplication1 %F
- As root, update the MIME database for your changes to take effect:
#
update-mime-database /usr/share/mime
- As root, update the application database:
#
update-desktop-database /usr/share/applications
- To verify that you have successfully associated
*.xyz
files with theapplication/x-newtype
MIME type, first create an empty file, for exampletest.xyz
:$
touch test.xyz
Then run thegvfs-info
command:$
gvfs-info test.xyz | grep "standard::content-type"
standard::content-type: application/x-newtype - To verify that
myapplication1.desktop
has been correctly set as the default registered application for theapplication/x-newtype
MIME type, run thegvfs-mime --query
command:$
gvfs-mime --query application/x-newtype
Default application for 'application/x-newtype': myapplication1.desktop Registered applications: myapplication1.desktop Recommended applications: myapplication1.desktop
12.3.3. Adding a Custom MIME Type for Individual Users
To add a custom MIME type for individual users and register a default application for that MIME type, you need to create a new MIME type specification file in the
~/.local/share/mime/packages/
directory and a .desktop
file in the ~/.local/share/applications/
directory.
Procedure 12.4. Adding a Custom application/x-newtype
MIME Type for Individual Users
- Create the
~/.local/share/mime/packages/application-x-newtype.xml
file:<?xml version="1.0" encoding="UTF-8"?> <mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"> <mime-type type="application/x-newtype"> <comment>new mime type</comment> <glob pattern="*.xyz"/> </mime-type> </mime-info>
The sampleapplication-x-newtype.xml
file above defines a new MIME typeapplication/x-newtype
and assigns file names with the.xyz
extension to that MIME type. - Create a new
.desktop
file named, for example,myapplication1.desktop
, and place it in the~/.local/share/applications/
directory:[Desktop Entry] Type=Application MimeType=application/x-newtype Name=My Application 1 Exec=myapplication1 field_code
The samplemyapplication1.desktop
file above associates theapplication/x-newtype
MIME type with an application named My Application 1, which is run by the commandmyapplication1
.Based on howmyapplication1
gets started, choose one respective field code from Desktop Entry Specification. For example, for an application capable of opening multiple files, use:Exec=myapplication1 %F
- Update the MIME database for your changes to take effect:
$
update-mime-database ~/.local/share/mime
- Update the application database:
$
update-desktop-database ~/.local/share/applications
- To verify that you have successfully associated
*.xyz
files with theapplication/x-newtype
MIME type, first create an empty file, for exampletest.xyz
:$
touch test.xyz
Then run thegvfs-info
command:$
gvfs-info test.xyz | grep "standard::content-type"
standard::content-type: application/x-newtype - To verify that
myapplication1.desktop
has been correctly set as the default registered application for theapplication/x-newtype
MIME type, run thegvfs-mime --query
command:$
gvfs-mime --query application/x-newtype
Default application for 'application/x-newtype': myapplication1.desktop Registered applications: myapplication1.desktop Recommended applications: myapplication1.desktop
12.3.4. Overriding the Default Registered Application for All Users
The
/usr/share/applications/mimeapps.list
and /usr/share/applications/[desktop environment name]-mimeapps.list
file are the package-installed defaults, which specify which application is registered to open specific MIME types by default.
To override the system defaults for all users on the system, system administrators need to create the
/etc/xdg/mimeapps.list
or /etc/xdg/[desktop environment name]-mimeapps.list
file with a list of MIME types for which they want to override the default registered application.
The order in which the configurations are applied is as follows:
/usr/share/applications/
/etc/xdg/
Within a particular location, the configurations are applied in this order:
mimeapps.list
[desktop environment name]-mimeapps.list
System administrator's configuration thus takes precedence over package configuration. And within each, desktop-specific configuration takes precedence over the configuration that does not specify the desktop environment.
Note
Red Hat Enterprise Linux versions prior to 7.5 used the
defaults.list
file instead of the mimeapps.list
file.
Procedure 12.5. Overriding the Default Registered Application for All Users
- Consult the
/usr/share/applications/mimeapps.list
file to determine the MIME types for which you want to change the default registered application. For example, the following sample of themimeapps.list
file specifies the default registered application for thetext/html
andapplication/xhtml+xml
MIME types:[Default Applications] text/html=firefox.desktop application/xhtml+xml=firefox.desktop
The default application (Firefox) is defined by specifying its corresponding.desktop
file (firefox.desktop
). The default location for other applications'.desktop
files is/usr/share/applications/
. - Create the
/etc/xdg/mimeapps.list
file. In the file, specify the MIME types and their corresponding default registered applications:[Default Applications] text/html=myapplication1.desktop application/xhtml+xml=myapplication2.desktop
This sets the default registered application for thetext/html
MIME type tomyapplication1.desktop
, and the default registered application for theapplication/xhtml+xml
MIME type tomyapplication2.desktop
.For these settings to function properly, ensure that both themyapplication1.desktop
andmyapplication2.desktop
files are placed in the/usr/share/applications/
directory. - You can use the
gvfs-mime query
command to verify that the default registered application has been set correctly:$
gvfs-mime query text/html
Default application for 'text/html': myapplication1.desktop Registered applications: myapplication1.desktop firefox.desktop Recommended applications: myapplication1.desktop firefox.desktop
12.3.5. Overriding the Default Registered Application for Individual Users
The
/usr/share/applications/mimeapps.list
and /usr/share/applications/[desktop environment name]-mimeapps.list
file are the package-installed defaults, which specify which application is registered to open specific MIME types by default.
To override the system defaults for individual users, you need to create the
~/.local/share/applications/mimeapps.list
or ~/.local/share/applications/[desktop environment id]-mimeapps.list
file with a list of MIME types for which you want to override the default registered application.
The order in which the configurations are applied is as follows:
/usr/share/applications/
/etc/xdg/
~/.local/share/application/
Within a particular location, the configurations are applied in this order:
mimeapps.list
[desktop environment name]-mimeapps.list
User's configuration thus takes precedence over system administrator's configuration, and system administrator's configuration takes precedence over package configuration. And within each, desktop-specific configuration takes precedence over the configuration that does not specify the desktop environment.
Note
Red Hat Enterprise Linux versions prior to 7.5 used the
defaults.list
file instead of the mimeapps.list
file.
Procedure 12.6. Overriding the Default Registered Application for Individual Users
- Consult the
/usr/share/applications/mimeapps.list
file to determine the MIME types for which you want to change the default registered application. For example, the following sample of themimeapps.list
file specifies the default registered application for thetext/html
andapplication/xhtml+xml
MIME types:[Default Applications] text/html=firefox.desktop application/xhtml+xml=firefox.desktop
The default application (Firefox) is defined by specifying its corresponding.desktop
file (firefox.desktop
). The system default location for other applications'.desktop
files is/usr/share/applications/
. Individual users'.desktop
files can be stored in~/.local/share/applications/
. - Create the
~/.local/share/applications/mimeapps.list
file. In the file, specify the MIME types and their corresponding default registered applications:[Default Applications] text/html=myapplication1.desktop application/xhtml+xml=myapplication2.desktop
This sets the default registered application for thetext/html
MIME type tomyapplication1.desktop
, and the default registered application for theapplication/xhtml+xml
MIME type tomyapplication2.desktop
.For these settings to function properly, ensure that both themyapplication1.desktop
andmyapplication2.desktop
files are placed in the/usr/share/applications/
directory. - You can use the
gvfs-mime --query
command to verify that the default registered application has been set correctly:$
gvfs-mime --query text/html
Default application for 'text/html': myapplication1.desktop Registered applications: myapplication1.desktop firefox.desktop Recommended applications: myapplication1.desktop firefox.desktop