Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
1  
you mean open pdf showing gui application ? or pdf reader for terminal ? –  geoh Feb 20 '13 at 17:21
add comment

8 Answers

up vote 57 down vote accepted

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
share|improve this answer
    
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
add comment
  • 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
share|improve this answer
    
thank you for this additional information! –  David Halter Jun 30 '12 at 18:12
    
What is xs? ... –  Keith Thompson Jun 30 '12 at 18:58
    
@keith X is the Xorg-server window handler, or somthing like that, both KDE and Gnome runs on top of X in most linux –  Puggan Se Jun 30 '12 at 19:00
2  
@PugganSe: I see. I've submitted an edit changing "xs" to "X's", which I think is much clearer. –  Keith Thompson Jun 30 '12 at 21:07
add comment

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

share|improve this answer
6  
gnome-open is non-standard and gnome specific, IMHO xdg-open is a better idea. –  Javier Rivera Feb 20 '13 at 17:23
add comment

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.

share|improve this answer
add comment

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 &
share|improve this answer
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
add comment

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

xdg-open somefile.pdf
share|improve this answer
add comment

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
share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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