How to use the command xdg-mime (with examples)

How to use the command xdg-mime (with examples)

The command “xdg-mime” is used to query and manage MIME types according to the XDG standard. MIME types are used to identify the type of a file, which helps applications determine how to open or handle the file.

Use case 1: Display the MIME type of a file

Code:

xdg-mime query filetype path/to/file

Motivation: To determine the MIME type of a file, you can use the “xdg-mime” command with the “query filetype” subcommand followed by the path to the file. This can be useful when you want to check the type of a file without relying on its file extension.

Explanation:

  • xdg-mime: The command to query and manage MIME types.
  • query: The subcommand used to retrieve information.
  • filetype: The keyword used to specify that we want to query the MIME type of a file.
  • path/to/file: The path to the file for which you want to determine the MIME type.

Example output:

text/plain

Use case 2: Display the default application for opening PNGs

Code:

xdg-mime query default image/png

Motivation: To find out the default application that opens a specific type of file, you can use the “xdg-mime” command with the “query default” subcommand followed by the MIME type of the file. This is helpful when you want to know which application will be launched when you open a file of that type.

Explanation:

  • xdg-mime: The command to query and manage MIME types.
  • query: The subcommand used to retrieve information.
  • default: The keyword used to specify that we want to query the default application for opening a file.
  • image/png: The MIME type of the PNG file.

Example output:

org.gnome.gthumb.desktop

Use case 3: Display the default application for opening a specific file

Code:

xdg-mime query default $(xdg-mime query filetype path/to/file)

Motivation: Sometimes you may want to find out the default application for opening a specific file, regardless of its file extension. To achieve this, you can combine the “xdg-mime” command with both the “query filetype” and “query default” subcommands.

Explanation:

  • xdg-mime: The command to query and manage MIME types.
  • query: The subcommand used to retrieve information.
  • default: The keyword used to specify that we want to query the default application for opening a file.
  • $(xdg-mime query filetype path/to/file): This command substitution retrieves the MIME type of the file by executing the “xdg-mime query filetype” command and passing the file path as an argument.

Example output:

org.gnome.gedit.desktop

Use case 4: Set imv as the default application for opening PNG and JPEG images

Code:

xdg-mime default imv.desktop image/png image/jpeg

Motivation: If you want to change the default application for opening specific file types, you can use the “xdg-mime” command followed by the “default” subcommand, the desktop entry of the application, and the MIME types you want to associate with that application.

Explanation:

  • xdg-mime: The command to query and manage MIME types.
  • default: The subcommand used to set the default application for opening specific file types.
  • imv.desktop: The desktop entry of the application we want to set as the default.
  • image/png image/jpeg: The MIME types we want to associate with the “imv” application.

Example output: (No output will be displayed if the command is successful)

Conclusion:

The “xdg-mime” command is a powerful tool for querying and managing MIME types according to the XDG standard. With its various subcommands and arguments, you can easily retrieve information about file types and default applications, as well as modify the associations between file types and applications. Understanding how to use this command can help you customize your system and improve your workflow.

Related Posts

How to use the command usermod (with examples)

How to use the command usermod (with examples)

The usermod command is used to modify a user account on a Linux system.

Read More
How to use the command 'entr' (with examples)

How to use the command 'entr' (with examples)

The entr command is a useful tool for running arbitrary commands when files change.

Read More
How to use the command "if" (with examples)

How to use the command "if" (with examples)

The “if” command in batch scripting is used to perform conditional processing.

Read More