How to easily open a PDF file from command line in Ubuntu
What do you do when you want to open a PDF file in Ubuntu? 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 16.04LTS.
Alternate way of opening a PDF file from command line
Needless to say, whenever we discuss a way to do a particular job through the command line, there's always a command line utility involved. That holds true in this case as well, as there exists a utility dubbed gnome-open that will help you easily open PDF files from the command line. The aforementioned tool can be downloaded and installed using the following command:
sudo apt-get install libgnome2-bin
Once installed successfully, all you have to do is to execute the following command to open a PDF file:
That's it. No need to keep in mind that there's a tool named Evince that handles PDF files, as gnome-open will automatically launch Evince for you.
Cool, isn't it? But now, some of you might have a couple of gnome-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 gnome-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. gnome-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 gnome-open is a generic utility that can open any type of file for you. And that makes sense as well, as otherwise you'll have to remember gnome-open instead of Evince for PDF files - the same problem that we started with.
So, if the file is PDF, gnome-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 gnome-open usage examples that you'll find useful:
To open a directory in Nautilus file manager, run the following command:
For example, the following command will open the current working directory in Nautilus:
And the following command will open the /tmp directory
To open a website in your system's default web browser, just pass the URL as argument to gnome-open. Here's an example:
Following are some other examples mentioned on the utility's man page:
Open an OpenOffice.org document
Open a directory in Nautilus
Send an e-mail
Possible useless use of gnome-open
And following is the list of command line options that the tool offers:
Show a brief help message
Display a brief usage message
Bonobo activation Support
File descriptor to print IOR on
IID to activate
Prevent registering of server with OAF
Disable sound server usage
Enable sound server usage
Host:port on which the sound server to use is running
Prints the version number
Please note that the KDE version of gnome-open is is kde-open. And there's also a universal version - dubbed - xdg-open - that works irrespective of the desktop environment being used.
In a nutshell, if you want to open a file or URL using default application from the command line, you should use gnome-open or other similar utilities. These utilities can also be used in case 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.
The utility gnome-open is easy to download and install, and its usage isn't complex as well. In most of the cases you will not require any of its command line options, but obviously, 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 armoury.