Open a directory in the default file manager and select a file

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.


You may want to consider using dbus to open your file as it is quickly becoming more popular.

The concept of a "default" file manager only really exists if you are solely focused on xdg-mime, however in dbus land, and indeed the majority of applications out there, the story takes a different twist, and the concept of a "default" file manager ceases to exist.

This is how you open a file (/home/me/path/to/folder/or/file) in the (default?) file manager, using dbus:

dbus-send --session --print-reply --dest=org.freedesktop.FileManager1 --type=method_call /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:"file:///home/me/path/to/folder/or/file" string:""

What this command does is to look for any dbus service which implements the org.freedesktop.FileManager1 interface, and calls it with the path to the file you wish to open.

The reason I put a question mark after default is because dbus does not respect the concept of "default" file manager; all it does is send the call to the first service it finds that implements the interface and lets it handle the operation. Now it may be that the application it picks is your "default" file manager, but that's not always a guarantee.

Most modern applications will use dbus by default and then fallback to xdg-mime if the dbus call fails, so this is what I'd suggest you do.

Tags:

Xdg