Command Line – Is There an ‘Open With’ Command?

desktop-environmentfile openingfilesfreedesktopmime-types

Does the command line have a way to get a recommended list of programs used to open a particular file, based on the file type? For example, a .pdf file would have an open with... recommendation using the programs Evince and Document Viewer.

I use the command line for most things, but sometimes I forget the name of a program that I want to use to open a particular type of file.

BTW I am using Ubuntu 13.10.

pro-tip

Thanks to @slm 's selected answer below, I made the following bash script in a file called openwith.sh:

xdg-mime query default $(xdg-mime query filetype $1)

Add as an alias or execute directly as an openwith command.

Best Answer

There isn't a command that I've ever seen that will act as "open with..." but you can use the command xdg-open <file> to open a given <file> in the application that's associated with that particular type of file.

Examples

Opening a text file:

$ xdg-open tstfile.txt
$

Resulting in the file tstfile.txt being opened in gedit:

                         ss of gedit

Opening a LibreOffice Writer document:

$ xdg-open tstfile.odt 
$

Resulting in the file tstfile.odt being opened in Writer:

                         ss of writer

What apps get used?

You can use xdg-mime to query the system to find out what applications are associated to a given file type.

$ xdg-mime query default $(xdg-mime query filetype tstfile.txt)
gedit.desktop calibre-ebook-viewer.desktop

$ xdg-mime query default $(xdg-mime query filetype tstfile.odt)
libreoffice-writer.desktop calibre-ebook-viewer.desktop

This is a 2 step operation. First I'm querying for the mime-type of a given file, xdg-mime query filetype tstfile.txt, which will return text/plain. This is then used to perform another lookup to find out the list of applications that are associated with this mime-type. As you can see above I have 2 apps associated, gedit and calibre, for .txt files.

You can use xdg-mime to change the associations too. See man xdg-mime for more details.

Related Question