If I click on a file in a GUI file explorer, a default program is used to open it. This is useful for files which I don't know how to open from the command line.
Is there a way to open a file with the default program using command-line instead?
Alternatively, is there a way, given a file extension, to determine a command to open it?
Best Answer
This is a job for
xdg-open
:Syntax for opening a file with it's default application:
xdg-open comes pre-installed in Ubuntu.
By the extension, no. The default application is chosen based upon the MIME type, not the extension. Linux does not have "extensions".
For a specific file, you can use
xdg-mime
to determine both the file's MIME type and the default application associated to it:Syntax for displaying a file's MIME type:
Syntax for displaying MIME type's default application:
Syntax for displaying a file's default application:
As a (hacky) workaround you could use the command
to display the default application for, e.g., PNG images.
This will work if and only if you have a PNG image on your computer and the first found file ending with
.png
is a valid PNG image.