Is there an "open with" command for the command line?

13,096

Solution 1

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.

Solution 2

If you mostly work on the command line, you could look at a curses-based file manager, like ranger or vifm. Both allow you to define default actions for filetypes.

In vifm, for example, in ~/.vifm/vifmrc you can define associations like so:

" Images
filetype *.jpg,*.jpeg,*.gif,*.tif,*.png,*.bmp sxiv 

" Media
filetype *.flv,*.avi,*.mp4,*.mpeg,*.mpg,*.mov,*.ogg,*.ogv,*.mkv mpv 

" Web
filetype *.html,*.htm,*.shtml /home/jason/Scripts/vimprobtab.sh 

" PDF
filetype *.pdf apvlv

Hitting Enter whith the cursor on any file with one of the defined actions will see it opened by the relevant application. As you can see in the case of .html files, you can trigger a script as well as an application.

Solution 3

Sort of, but it will change your default application as a result. I'm not sure what other operating systems this works on, but the instructions below work for Ubuntu 12.04 - Desktop X86-64. I didn't have any pdf files handy so I tested with a .zip archive.

General Steps

Step #1

In a terminal type:

$ mimeopen -d /home/username/example.zip
screenshot #1 = https://copy.com/qfWSZaZ4FzlA

    ss #1

Step #2

Choose from the list by entering the # of the application you want, and pressing enter. The file will immediately open in the application you chose.

screenshot #2 = https://copy.com/um6Rf7zRdceT

    ss #2

screenshot #3 = https://copy.com/ytwKCqR6nv8i

    ss #3

Notes

Note #1

This changes the default application to the one you choose, and so any time you open that file type it will now open in whatever application you last chose from the list.

Solution 4

Depends on the flavor of Unix you're using, I guess. In OS X, you can use the open command:

OPEN(1)                   BSD General Commands Manual                  OPEN(1)

NAME
     open -- open files and directories

SYNOPSIS
     open [-e] [-t] [-f] [-F] [-W] [-R] [-n] [-g] [-h] [-b bundle_identifier]
          [-a application] file ... [--args arg1 ...]

DESCRIPTION
     The open command opens a file (or a directory or URL), just as if you had
     double-clicked the file's icon. If no application name is specified, the
     default application as determined via LaunchServices is used to open the
     specified files.

     If the file is in the form of a URL, the file will be opened as a URL.

...
Share:
13,096

Related videos on Youtube

modulitos
Author by

modulitos

Updated on September 18, 2022

Comments

  • modulitos
    modulitos almost 2 years

    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.

    • Admin
      Admin about 10 years
      You can try with gnome-open file. I have added alias o='gnome-open' to my .bashrc for simplicity.
    • Admin
      Admin about 10 years
      gnome-open works similarly to xdg-open.
    • Admin
      Admin about 10 years
      Apparently, so does gvfs-open. Question What can I use instead of gnome-open? from ask ubuntu.
  • Ionoclast Brigham
    Ionoclast Brigham about 10 years
    Also, the open command first appeared in NextStep and was inherited by OS X, so it may be available in some form on other more or less OpenStep-based platforms (e.g. AfterStep, LiteStep).
  • galzra
    galzra about 10 years
    Did you mean to put a hash sign (#) in the URL for screenshot 3?
  • please delete me
    please delete me about 10 years
    @trysis I just used it to bypass the max 2 urls limit.
  • galzra
    galzra about 10 years
    Oh, makes sense. Still looks weird, though.
  • slm
    slm about 10 years
    I cleaned up you A and added the 3rd URL for you.
  • modulitos
    modulitos about 10 years
    Is there a way to display other kinds of programs besides the default ones? For example, I just installed okular to view PDF's, but your example above only shows evince when I run it on a .pdf file.
  • slm
    slm about 10 years
    @Lucas - yes the examples I've shown show the applications that are associated with a given file type above. For ex. files of type .txt are associated with gedit and calibre for example.
  • modulitos
    modulitos about 10 years
    Do I need to have .desktop files for the programs that I want listed? For example, when I run the script on a .pdf file, it returns evince.desktop, even though I can run okular as well.
  • slm
    slm about 10 years
    @Lucas - this is probably best asked as a new Q, since we're meandering away from the original Q now.