235

I've found the "open" command in Mac OS X very handy in the command line. From "man open":

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.

That is, if I want to open a PDF file with the default PDF viewer (happens to be Preview), I only need to do:

open my.pdf

In Linux, however, to open a PDF file from the command line, I had to dig around to find the default PDF viewer is, for instance, "evince" (who'd have guessed??), and then

evince my.pdf

So, is there a simple equivalent of the 'open' command in the Linux command line?

Thanks!

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 7
    Maybe this question should be moved over to superuser.com? – quazgar May 26 '13 at 13:58
  • 1
    Super User seems inappropriate - is there a StackOverflow for "ordinary users" or possibly "programmers who want to script other applications", i.e. automate? – Krazy Glew Sep 12 '16 at 02:02
  • 1
    askubuntu might fit. – Gopherkhan Feb 17 '17 at 00:01
  • It seems perfectly relevant to Stack Overflow to me. I'm using it in a script I'm writing to support opening URLs to work on multiple platforms. This isn't the first time I've wound up here from Google either. – Taylor D. Edmiston Feb 07 '22 at 17:06

7 Answers7

263

You could try xdg-open, most Linux distros have it. It will open default associated app for your file.

FYI https://portland.freedesktop.org/doc/xdg-open.html

Nathan Musoke
  • 166
  • 1
  • 12
number5
  • 15,913
  • 3
  • 54
  • 51
  • 14
    A big difference between this and `open`, however, is that this doesn't open the file/app in the background. Linux apps seem to be very chatty and often output multiple lines of diagnostics and warnings right into your terminal (even though nothing really went wrong). I usually have to do something like `xdg-open &> /dev/null &` instead. Is there anything better than xdg-open in this regard? – Suan Nov 30 '11 at 04:43
  • 14
    @Suan: It does open the app in the "background", detached from terminal, in a way you may continue working in the terminal as soon as the app is launched. So the trailing `&` is not needed. As for the "multiple lines of diagnostics", those are from the app itself, not from `xdg-open`. If you don't want any output, you would need `&> /dev/null` even using `evince` directly (OP's example), so your complain is void. `xdg-open` is indeed the way to go. – MestreLion Sep 11 '12 at 21:18
  • 17
    @MestreLion I don't think the complaint is entirely void, since Mac's open does not result in application output being echoed to the terminal. – Max Nanasy Nov 16 '12 at 00:31
  • 7
    @MaxNanasy: output is from applications, it is not `xdg-open`'s fault if *they* are chatty. And suppressing such messages should be *user's* choice, not xdg's. So it is a void complain to ask for something "better" than `xdg-open`. Also, if Mac's `open` has no option to output those messages, I'd say that `open`is *much worse* than `xdg-open`, since when you use command line to launch files you're usually interested in that output (otherwise, why not simply double-clicking the file in a browser like Nautilus?) – MestreLion Nov 16 '12 at 05:32
  • 6
    @MestreLion The use case I, and probably most Mac users, most often use `open` for is if I'm already using a terminal, but want to open a file with a GUI application (e.g. I'm in a folder with an image I want to view, so I run `open image.png`, rather than re-navigating to the folder in Finder). If I do want to view debug messages from a GUI application, I open Console, which has a Console Log view that shows messages from all running applications. – Max Nanasy Nov 16 '12 at 09:38
  • 3
    @MestreLion PS: I'm not saying that `open` is better or worse than `xdg-open`: I'm just denying that Suan's complaint is void, since the two systems have different behavior, and Suan prefers one aspect of one system's behavior. – Max Nanasy Nov 16 '12 at 09:42
  • 15
    @MestreLion Although one could make the case that the complaint is trivial, since `alias open='xdg-open &>/dev/null'` yields approximately the desired behavior (although errors from xdg-open itself (as opposed to just the launched program) would also be silenced, which is undesirable). – Max Nanasy Nov 16 '12 at 10:04
  • @MaxNanasy: we both agree that the only difference in each tool is their default behavior regarding output: `open` by default silences output (is there a way to make "un-silence" it?), while `xdg-open`, as most linux terminal tools, does not silence its application's output. And it is a sensible default, IMHO, since A) It is trivial to disable it yourself, if you want to; B) apps may output important messages, not only debug diags and warnings – MestreLion Nov 16 '12 at 10:36
  • 1
    xdg-open doesn't open multiple files like "open" on OSX: `open image*.jpg` A workaround may be needed: `ls *.jpg | while read -r file; do xdg-open "$file"; done` – anh_ng8 Apr 15 '15 at 17:09
  • 4
    how about "open -a", im trying to solve this problem but on linux http://apple.stackexchange.com/questions/75221/how-to-drop-a-file-onto-a-gui-app-from-command-line – frazras Oct 11 '15 at 21:48
  • Mac name it 'open'. Ubuntu name it 'xdg-open'. Who is the one give that ugly name. Why not using like 'zSy-#d%^-open' to make it harder another level, for something that so trivial. – nafsaka Jun 17 '21 at 10:11
  • @nafsaka one reason might be that `open` is already referenced on multiple sections in the man pages as a c function and `xdg-open` didn't want to conflict with them. *shurgs – Timo Sep 12 '21 at 17:15
82

The equivalent you are looking for is xdg-open, which can be used in the same way as OS X's open command. For example:

xdg-open ~/Documents/Chubby_Bubbies.odt

However, this is really hard to type quickly and accurately. Instead, you should make an alias to xdg-open, which makes the process much quicker.

Of course, you can alias it to open to make it match OS X (you can pick anything you want), but personally, I use the right square bracket (]) for my shortcut for speed reasons. To use this, add the following to your .bashrc file:

alias ']'='xdg-open'

Then, to open any resource, use it like any of these examples:

] www.google.com
] file.txt
] ~/Pictures
] ssh://myserver.local/home/jeremy

Also this lets you open a file browser (e.g. Nautilus) in the current directory:

] .

From experience I have found that one-letter aliases work best for the above shortcut. After all, the goal is efficiency. And you can go back and make the same alias on OS X — I leave that as an exercise to the reader. :-)

Jeremy Visser
  • 6,237
  • 1
  • 25
  • 30
  • 26
    I made an alias from 'open' to 'xdg-open' in order to have it consistent on both Linux and Mac. – Adam Byrtek Oct 20 '09 at 21:19
  • Thats good but not great, I tried to open an .url file and opened it in gedit. – sorin Mar 28 '12 at 09:55
  • 7
    @sorin : `xdg-open` opens your system's default application for the file. In your case, `.url` files are set to open in gedit. If you want to change that, right-click the file in Nautilus and go to Properties > Open With to change the default application, and `xdg-open` will work accordingly. – MestreLion Sep 11 '12 at 21:28
  • [Here is a Gist](https://gist.github.com/ozanmuyes/ea92b8eaba34d9c57894f05751270118) that I've made, just copy-paste and execute command under 'Altogether' section in the first comment. – ozanmuyes Jun 17 '17 at 02:03
  • for Chubby Bubbies! lol nifty alias @ozanmuyes cool script, I [ported to zsh](https://gist.github.com/lacostenycoder/3ad934aab2017bcb788deb66778ef26d), there was a minor bug but it didn't break anything, works. – lacostenycoder Oct 21 '21 at 15:14
17

I just sorted this out myself so thought I would write down how I did it, which is specifically relevant to what Suan asked. These steps allow you just type "open " and not your terminal covered in messages you don't need:

Create a script called open in ~/bin, the content is just:

xdg-open "$1" &> /dev/null &

Save and close the script, then type "source .profile" (or .bash_profile if relevant). Thats it so typing "open Music" will open your music folder in the nautilus GUI and shouldn't enter anything onto your terminal.

MestreLion
  • 12,698
  • 8
  • 66
  • 57
Anake
  • 7,201
  • 12
  • 45
  • 59
  • What's with the alias and the weird location? Just call the script itself `open` and put it in your PATH (typically in `$HOME/bin/open`) – tripleee Mar 28 '12 at 14:59
  • 1
    On my machine (ubuntu 11.10) if you add a script called 'open' to your path, and try to do that way it is overidden by /bin/open. So I can't work out a way of getting your way to work without an alias. – Anake Apr 15 '12 at 19:29
  • The trick is to have your own script in a directory which comes earlier in your `PATH` than `/bin`. By convention, if you have `$HOME/bin` it would be added to the beginning of the PATH, before any system directories. Commonly, this is done by your `.profile` or similar. For example, the default `.profile` on Ubuntu does exactly this. – tripleee Apr 23 '12 at 10:57
  • 2
    Redirecting output to `/dev/null` is a bit capricious; perhaps you could redirect output to `$HOME/.xsession-errors` or some such, so that diagnostics are not completely lost in case you need to troubleshoot something. – tripleee Apr 23 '12 at 15:24
  • I had to use "$1" instead of $1 (including the quotes) to stop xdg-open from being confused about spaces in filenames. – efdee Aug 26 '12 at 14:24
  • 1
    @efdee: properly quoting *all* variables is a good programming practice, and vital in bash scripts. This has nothing to do with `xdg-open`, this is a shell issue. – MestreLion Sep 11 '12 at 23:15
  • Also, the `&` in the end is not needed: `xdg-open` in gnome uses `gvfs-open`, which already opens applications in the background. And IMHO the redirection should not be used: opening files in terminal usually means you *are* interested in debug/error messages. Otherwise, why not simply use Nautilus instead? – MestreLion Nov 16 '12 at 05:43
  • On Mac Finder remembers the view settings (sidebar, toolbar, view mode etc) per directory. I think it stores those in a hidden file (.DS_Store perhaps?). It even remembers the state of opened/closed items in the tree view! So when I worked on a Git repo with Vim, I usually typed "open ." to get started. It would open a nice treeview without all the cruft (sidebar/toolbar), and it would always open where it was the last time. Essentially a really nice and handy way of browsing my repo and drag/dropping files into Vim (I love Vim but I hate the stupid text mode windows). –  Apr 17 '13 at 03:51
  • The `xdg-open` doesn't always run it in the background, I'm currently using KDE. Also `xdg-open` isn't exactly equivalent to `open` or Cygwin's `cygstart`. The application being opened should open completely independently from the terminal, and not be part of the process tree of the terminal, which means it should be detached, and its error output should flow to `~/.xsession-errors`. – CMCDragonkai Apr 17 '16 at 09:01
  • Also `xdg-open` doesn't open `*.desktop` files currently. So I can't just open an application as is, without opening a file association. – CMCDragonkai Apr 17 '16 at 09:01
  • In my ~/.zshrc I've added: `alias open="xdg-open &>/dev/null "`. – Nick Desaulniers Apr 18 '16 at 23:57
11

Traditionally, you can use the "see" command. Which just uses run-mailcap. This will work without Gnome and X etc.

man see
Ali Afshar
  • 40,967
  • 12
  • 95
  • 109
  • 3
    The problem is `run-mailcap` (and its many aliases) uses a separate (and terribly outdated) app association database. For example in my system, while `.mp3` songs correctly open in VLC, `.txt` files open in less (instead of gedit) and `.pdf` files simply fail to open! ( Error: no "view" mailcap rules found for type "application/pdf") – MestreLion Sep 11 '12 at 21:36
  • As I said, it depends on each system. Few applications register themselves in a way `run-mailcap` recognizes them. `xdg-open` internally uses `gvfs-open` (on Gnome systems like Ubuntu), which in turn uses xdg's mime-database spec, which far more applications use. – MestreLion Sep 11 '12 at 23:19
  • Opened a 50+ page PDF in `gimp` for me. I'll stick with `xdg-open`. – user1717828 Oct 23 '17 at 15:36
  • Opening txt files in `less` x''D – Timo Jun 06 '20 at 12:36
4

gnome-open

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • 7
    gnome-open is now deprecated, and besides it was already used internally by `xdg-open` if the user was on Gnome (now it uses `gvfs-open`) – MestreLion Sep 11 '12 at 21:38
2

If you have your mimes setup correctly, you can use mimeopen. Check out its man page, since it has some useful options.

stacksia
  • 631
  • 6
  • 10
-3

Under Gnome Desktop environment, I use the following command:

nautilus `pwd` &

This is similar as "open ." command in Mac

Niall C.
  • 10,878
  • 7
  • 69
  • 61
WonderLi
  • 31
  • 2
  • 11
    This answer has multiple issues: A) It opens the current directory, not the file, as user requested. B) you can use `.` instead of "``pwd``" C) if you change `pwd` for a file, user would still need to hit ENTER after Nautilus opens D) Opening nautilus first just for *it* to open the file is a very indirect solution. – MestreLion Nov 16 '12 at 05:39