Opening files in the default application from the terminal in 2023
Nearly ten years ago, in August two thousand fourteen, I wrote a short article on how to open a file from the command line in the default application assigned to the file type. Since then, the correct approach to this problem has managed to change several times. In this article, I'll try to update that knowledge and show you how to get started in 2023.
Video presentation
Before I get into the nitty-gritty, I will post a link to a video illustrating the content of this article if anyone is more comfortable with this format.
Get to work!
Let's imagine the situation that we have an image file in a directory, and we want to open it in the default application from a terminal.
$ ls owl.jpg
If we haven't made any changes, for Ubuntu with the GNOM desktop environment, this will be the Eye of GNOME application - eog. However, this does not have to be the case, and we do not need to know the target application. We are interested in opening the file in the default application, without knowing or figuring out what that application is.
In the aforementioned article, I wrote about the possibility of using the gnome-open command. However, as we can see, it is no longer available.
$ gnome-open owl.jpg gnome-open: comand not found
Historically, this has been replaced by the gvfs-open command. Although this command still works and the image file will open, you will see a warning in the terminal that we should use yet another command, namely gio, which is short for GNOME Input/Output. By the way, this command has much broader capabilities than just opening a file in a dedicated program for its type, but that's a topic for a completely different video. We will focus strictly on our task.
$ gvfs-open owl.jpg This tool has been deprecated, use 'gio open' instead. See 'gio help open' for more info.
A call to the gio command would look like the following:
gio open owl.jpg
This solution, however, is dedicated exclusively to the GNOME desktop environment. If you are interested in a solution that works independently of the desktop environment, a good alternative would be the xdg-open command. We will use it very similarly:
xdg-open owl.jpg
Conclusion
This leaves us to conclude that, for the gnome desktop environment, we can confidently use the `gio open` command, and for all other cases, it is worth remembering the `xdg-open` command.
Komentarze (2)