Friday, May 8, 2009

Open a file from the command line using its default application

Windows users are familiar with the concept of file association. When you double click a file (say, cisco.doc), Windows examines the file name extension (doc), and opens the file using the default program associated with that extension(Office).

Linux users can open files in a similar way in their X Window graphical user interface. But, if you want to open the file from the command-line, you need to type out at least the program name, oowriter, or do you?
$ oowriter cisco.doc 


If GNOME is your window manager, use the gnome-open command as follow:
$ gnome-open cisco.doc 


oowriter is automatically started up to open cisco.doc, if oowriter is indeed associated with the .doc file name extension.

For KDE users, use kde-open instead.

Alternatively, you can run the window-manager-neutral program called xdg-open. xdg-open is part of the xdg-utils package.

$ xdg-open cisco.doc

13 comments:

  1. Another (better?) solution is to use ZSH aliases. Just add a line like "alias -s doc='soffice'" to your .zshrc and you will be able to just type the name of the file:

    $ cisco.doc

    ReplyDelete
  2. thanks Peter, your blog helped me in my search.
    I didn't know how to open the stuff from command line.

    ReplyDelete
  3. Thank you. Just what I was looking for.

    ReplyDelete
  4. nice post
    im searching exactly for this

    ReplyDelete
  5. Thanks, that was very helpful!

    ReplyDelete
  6. Thanks for the Info.!! :)

    ReplyDelete
  7. Thanks for the info, very helpful!

    ReplyDelete
  8. Thank you! This comment was really helpful

    ReplyDelete
  9. Thank you ! It was really helpful

    ReplyDelete
  10. Thanks. I've been dying for a way to do this since I discovered something similar when stuck on MacOS.

    ReplyDelete
  11. Excellent! That's what I have wanted to do! I just added

    alias o='kde-open'

    in my .bashrc ;-)

    ReplyDelete
  12. thank you @Culop where to put alias on linux

    on MacOSX (Tiger+) it is "open filename.doc"

    ReplyDelete