How to Open Files in Default Desktop Application from Command-Line on Ubuntu

What do you do when you want to open a PDF file in Ubuntu from command-line in the default app for this file type? Simple, double click on the PDF file icon, or right-click and select the "Open with Document Viewer" option. But what if you're asked to do the same task through the command line? Do you know the command line utility that will do the job for you?

Unless you are a command line pro, I am sure the answer will be "NO." FYI, Evince is the default document viewer for PDF and PostScript files in Ubuntu, and the utility of the same name does the job for you in case you want to open these type of files from the command line.

However, you'll be glad to know that there's way through which you can launch Evince for a PDF file, even if you don't know the fact that a command line utility of that name exists, and that's what we'll be discussing in this article.

Please note that all the commands and instructions mentioned in this article have been tested on Ubuntu 22.04.

Alternate way of opening a PDF file from command line

When we discuss a way to do a particular job through the command line, there's always a utility involved. That holds true in this case as well, as a utility dubbed xdg-open will help you easily open PDF files from the command line. You'll you have to do is to execute the following command to open a PDF file:

xdg-open [name-of-PDF-file]

That's it. No need to remember that there's a tool named Evince that handles PDF files, as xdg-open will automatically launch Evince for you.

Open PDF from command-line on Linux

Cool, isn't it? But now, some of you might have a couple of xdg-open related queries in your mind. Like, how the utility works, and does it only support PDF files. Well, let me answer these one by one.

As for how xdg-open works, it uses the GNOME file handlers to open the files and URLs passed to it as arguments. The process is similar to how the GNOME file manager identifies the default application to open a file with when you double-click the file's icon. xdg-open identifies the file's mime-type, launches the default application associated with that mime-type, and passes on the file name (that user wants to open) to that application.

For mapping between mime-types and default applications, head to the /etc/gnome/defaults.list file on your Ubuntu machine.

Now, coming to the second question, the explanation that I offered as part of the answer to the first question should have given you an idea that xdg-open is a generic utility that can open any file for you. And that makes sense as well, as otherwise you'll have to remember xdg-open instead of Evince for PDF files - the same problem that we started with.

So, if the file is PDF, xdg-open will open the file in Evince document viewer, and if it's a text file, Gedit will be launched.

Moving on, here are some xdg-open usage examples that you'll find useful.

Open Directory or Path in Desktop App from CMD

To open a directory in Nautilus file manager, run the following command:

xdg-open [dir-name-or-path]

For example, the following command will open the current working directory in Nautilus:

xdg-open .

And the following command will open the /tmp directory

xdg-open /tmp

Open folder in desktop file manager from command line

Open Website in Desktop Browser from Ubuntu Command-Line

To open a website in your system's default web browser, pass the URL as an argument to xdg-open. Here's an example:

xdg-open https://www.howtoforge.com/

Open website in desktop browser via command line

Following are some other examples:

       Open an OpenOffice.org document
             xdg-open Spinach.odf

       Open a directory in Nautilus
             xdg-open ~

       Send an e-mail
             xdg-open mailto:[email protected]

      Possible useless use of xdg-open
             xdg-open /usr/share/applications/gedit.desktop

And following is the list of command line options that the tool offers:

XDG Open

Conclusion

In a nutshell, if you want to open a file or URL using the default application from the command line, you should use xdg-open or other similar utilities. These utilities can also be used if you forget or don't know which command line utility to use for opening a particular file - the case we've discussed in this article. In most cases, you will not require any of its command line options, but, that won't always be the case, so we've mentioned the list here in the tutorial itself. All in all, a useful tool to have in your armory.

Share this page:

6 Comment(s)