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:

Anonymous said...

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

Anonymous said...

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

Ben said...

Thank you. Just what I was looking for.

Anonymous said...

Nice. Good post.

Anonymous said...

nice post
im searching exactly for this

Anonymous said...

Thanks, that was very helpful!

sanjeev said...

Thanks for the Info.!! :)

Anonymous said...

Thanks for the info, very helpful!

terry said...

Thank you! This comment was really helpful

Nazila said...

Thank you ! It was really helpful

Anonymous said...

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

Culip said...

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

alias o='kde-open'

in my .bashrc ;-)

K.D.G said...

thank you @Culop where to put alias on linux

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