In a program which I am writing I want to offer the functionality to open the directory where the file which I am currently processing is located and automatically select that file (so that the user does not need to search for it).

I know that I can open a directory in the default file manager using

xdg-open /path/to/directory

I know that I can open a directory in nautilus and select a file using

nautilus /path/to/file.txt

I thought that I could use

xdg-mime query default inode/directory

to get the default file manager and - if it is nautilus - call it as shown above. But, despite the fact that nautilus is the default on my system (xdg-open opens nautilus and so does the places menu in the gnome shell), xdg-mime returns Thunar.desktop. (I have tried find / -name Thunar.desktop -mount 2>/dev/null but it did not find anything.)

Also, I do not know how to open a directory and select a subdirectory in nautilus (with the above mentioned approach it would open the subdirectory).

How can I open a directory in the default filemanager and select a file in that directory (if selecting a directory, too, was possible that would be great, but for this application not needed) or at least find out the default filemanager so that I can call it directly?


1. To open a directory and select a subdirectory/file in nautilus:

nautilus --select path/to/file/or/directory

From nautilus(1) man page:

-s, --select
  Select specified URI in parent folder.

2. xdg-mime returns Thunar.desktop but xdg-open opens nautilus

xdg-mime uses mimeapps.list to determine the default application to use.

Separate mimeapps.list files exist to handle user-specific, system-specific and distribution-specific requirements. Their lookup order can be found over here.

mimeapps.list lists default applications for a given mimetype under [Default Applications] section. It allows to list multiple default applications in their decreasing order of preference. For example :

[Default Applications]
mimetype1 = default1.desktop;default2.desktop;

where mimetype1 is the mime type and *.desktop are the desktop files.

xdg-open searches for desktop file down the lookup order, across the preference list till it finds a valid desktop file. If no such file is found across all the files then the most preferred one according to the associations is chosen and is used as default application.

So in case of our example, let us suppose default1.desktop is not present on our system, so xdg-open will try to open our file using default2.desktop. However, xdg-mime returns default1.desktop which is the first entry in our mimeapps.list file.

In your case default1.desktop must be Thunar.desktop hence the output. However it is not installed on your system. So xdg-open opens your file/directories using nautilus which is present on your system. To verify this, you can check your mimeapps.list file for line containing inode/directory. For Ubuntu 17.10, the location of mimeapps.list file is : /usr/share/applications/defaults.list

NOTE: The complete algorithm to determine 'Default Applications' can be found here.

  • In case something has remained unanswered or if there is any confusion, drop a comment. – Amit Singh Dec 14 '17 at 7:16
  • 1
    Your excellent answer looked like a question on the first spot (what is a delete reason), it could mislead reviewers, so I edited it a little bit. Feel free to rollback if it is not okay. – peterh Dec 14 '17 at 8:19
  • @peterh Thanks for the edit! I will keep it in mind for future answers. – Amit Singh Dec 14 '17 at 9:15
  • +1. Thanks a lot for your answer. The first part works perfectly. The second part together with this specification explaining which values are assumed if the environment variables are not set explains why Thunar is returned. – jakun Dec 15 '17 at 7:31
  • 1
    However, I still can not retrace why Nautilus is started because it is not specified in a mimeapps.list. Can you explain what these associations are in "if after all files are handled, we have not yet found a default application, select the most-preferred application (according to associations) that supports the type"? Do you know a way how to find out which program will be started automatically whitout needing to reimplement this algorithm? – jakun Dec 15 '17 at 7:32

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.