78

This question already has an answer here:

I want to be able to open files like my operating system does from console.

e.g. a .pdf file should be opened with evince or whatever the default application to open pdfs is. All the other things should work too; images -> eog, text files -> gedit and so on.

I hope that there is a solution for this, it would be really practical.

marked as duplicate by muru, Eric Carvalho, Zanna, Ravexina, user364819 May 7 '17 at 9:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    you mean open pdf showing gui application ? or pdf reader for terminal ? – hingev Feb 20 '13 at 17:21
97

xdg-open — opens a file or URL in the user's preferred application

But what if you want to change the preferred application from the terminal?

Try this

You can use the xdg-mime program to first check which is the current default application that will open a file and then you can switch it to what ever application you want.

$ xdg-mime query default application/pdf
AdobeReader.desktop
$ xdg-mime default evince.desktop application/pdf
$ xdg-mime query default application/pdf
evince.desktop

Now as mentioned by jokerdino you can use xdg-open to open a file with your preferred application:

$ xdg-open file.pdf
  • I tried this on Ubuntu 11.10 from gnome-terminal and terminator and the output of the xdg-mime was that was written here, but the xdg-open showed the pdf file in nautilus (with setting evince or AdobeReader as well). – Arpad Horvath Jul 6 '12 at 19:25
  • 2
    It's handy to have a shell alias to this, so something like this alias x='xdg-open' and it's a simple x file.ext :) – Greg Sheremeta Dec 23 '15 at 4:15
  • xdg-mime refers to system wide. User ~/.config/mimeapps.list takes precedence over system settings. It will not work if nautilus change mimetype. – EdiD Mar 3 '16 at 19:15
56
  • gnome-open opens a file with Gnome's default application
  • kde-open opens a file with KDE's default application
  • xdg-open opens a file with X's default application
  • 1
    gnome-open has been replaced with gvfs-open – Alex Fedulov Oct 19 '16 at 17:06
  • 1
    'xdg-open' is not for X, per se. It's from the Free Desktop project (formerly known as the X Desktop Group, but not related to the X Windows project). Free Desktop's xdg-* commands are meant to be a desktop-neutral way of interacting with your workstation. So, whether you have Gnome, KDE, or something else, "xdg-open" will work. Free Desktop is a standard. It's entirely on figurative paper. It has no desktop environment of its own. – Todd Walton Jun 4 '18 at 13:13
22

Well if you mean command with what you can open everything, it will choose automatically, there is gnome-open

If you want to open .pdf or whatever file it is, just type:

gnome-open blah.pdf

And there is xdg-open for this kinda works. What it does: it just looks what is default application for such type of files, and runs that app.

In manual of xdg-open

xdg-open - opens a file or URL in the user's preferred application

Also from here

xdg-open is part of the xdg-utils package available in [extra]. xdg-open is for use inside a desktop session only. It is not recommended to use xdg-open as root.

And sure to change "default" application, you will need xdg-mime
Example to change default pdf viewer:

$ xdg-mime default xpdf.desktop application/pdf

Source: https://wiki.archlinux.org/index.php/Xdg-open

  • 6
    gnome-open is non-standard and gnome specific, IMHO xdg-open is a better idea. – Javier Rivera Feb 20 '13 at 17:23
17

To add to the existing points.

I find this command so important that I have an alias in ~/.bashrc:

alias go='xdg-open'

I also find the command xdg-open . or with the alias go . really useful. The period represents the working directory, and it opens Nautilus in the working directory. Thus, if you need to temporarily take advantage of features in Nautilus, such as opening files in alternate programs, deleting files with strange names, selecting strange combinations of files, etc. then you can do that in a relatively simple way.

11

You can use xdg-open, it should open a file with the default program:

xdg-open somename.pdf

will open somename with your default pdf reader.

Or, usually, you can just use the program name followed by the name (or path and name) of the file that you wish to open

evince somename.pdf

will open somename in evince.

If you want to be able to keep using the terminal while working with the file you can add & at the end of the command, it will be executed in the background and you can continue tying commands in the terminal. Otherwise you can't use the terminal until the program is closed.

xdg-open somename.pdf &
  • 2
    and you can add "&" after the file name so you can keep using the terminal: evince somename.pdf& – carnendil Feb 20 '13 at 17:26
6

You can use xdg-open to open any file type your desktop environment knows about, for example

xdg-open somefile.pdf
4

Very useful! I added a bit to this, and created a shell script "open". Essentially, if there is no argument to open, or if the argument to open is a directory, you use nautilus, else the xdg-open command:

bash version:

#! /bin/bash
#
#  this emulates the mac 'open' command, which figures out fromthe
#  file how to 'display' a file
#
#  gnome-open   kde-open    xdg-open

if [ $* > 0 ] ; then
  if [ -d $1 ] ; then
    nautilus $1
  else
    xdg-open $*
  fi
else
  nautilus .
fi

csh version:

#! /bin/csh -f
#
#  this emulates the mac 'open' command, which figures out fromthe
#  file how to 'display' a file
#
#  gnome-open   kde-open    xdg-open

if ($#argv > 0) then
  if (-d $1) then
    nautilus $1
  else
    xdg-open $*
  endif
else
  nautilus .
endif
1

There is a terminal file manager called Midnight Commander Install mc.

sudo apt-get install mc

It allows you to browse files and opens files with default applications. But it is more like a file manager not like the xdg-open

Not the answer you're looking for? Browse other questions tagged or ask your own question.