14

On macOS in the default Terminal running bash, I can type:

open -a Firefox http://www.wikipedia.org

to open a URL with Firefox, or another browser. It also works to launch a program, e.g. open -a maps, and to open a file in the default program, e.g. open file.pdf.

How can I do the same on Ubuntu in the default GNOME Terminal also running bash? I know that I can open a file or URL with xdg-open /path/to/file, and I can open the default browser with sensible-browser. Does Ubuntu have a general command to open files, applications, or files in specific applications?

I am running bash on both machines, 3.2.57(1)-release on macOS and 4.4.19(1)-release on Ubuntu.

Melebius
  • 11,431
  • 9
  • 52
  • 78
  • I've never used a Mac, so can you tell me why you'd use open -a Firefox http://www.wikipedia.org? Can't you do firefox http://www.wikipedia.org? It seems like a useless feature of open, when opening programs is the very point of bash. – JoL Oct 04 '18 at 15:26
  • 1
    Bash on macOS works differently, e.g. your command gives the error bash: firefox: command not found. Same error with Firefox as program name. – miguelmorin Oct 04 '18 at 16:36
  • 3
    bash works exactly the same; it's Firefox that is installed differently than it is on Ubuntu. On macOS, you could, for instance, run /Applications/Firefox.app/Contents/MacOS/firefox-bin from the command line. Firefox.app is a special kind of folder that contains not just the executable, but files that in Linux might be scattered about in various /usr/bin/, /usr/lib, /etc, etc. directories. The Finder knows how to run the actual application when you, for instance, double-click on Firefox.app. open is a way to "run" the special folder from the command line. – chepner Oct 04 '18 at 18:53
  • 4
    It's not that bash works differently: firefox is installed as an OS X application (Firefox.app) and is not on the regular PATH. Another reason to use open -a is that it properly backgrounds the application it launches, instead of waiting on it until it exits. – alexis Oct 04 '18 at 18:54
  • https://askubuntu.com/a/15356 & – Dev Oct 04 '18 at 20:00

2 Answers2

11

You can specifically use your desired program's name (if it's able to be used as a command line tool).

For example, urls:

firefox duckduckgo.com
chromium-browser askubuntu.com

PDF:

evince foo.pdf
okular bar.pdf

Images:

gpicview foo.png
feh bar.jpeg

Texts:

gedit foo.txt
mousepad /etc/config
leafpad bar.xml

Video/Music:

mpv  foo.mp3
vlc  bar.mp4

If you want the program to be run detached from the terminal then this is the way that I prefer doing it:

nohup program args &

For example:

nohup firefox askubuntu.com &

Remember that you can always redirect the outputs as usual, e.g. :

nohup firefox duckduckgo.com &> /dev/null &
Ravexina
  • 55,668
  • 25
  • 164
  • 183
  • 2
    eog picture.jpg for images too if you have gnome-desktop installed :) – Videonauth Oct 04 '18 at 11:19
  • 1
    @Videonauth Yeah, or any other kind of tools ;) – Ravexina Oct 04 '18 at 11:20
  • This is a somewhat acceptable answer but it just works around the question, doesn't it? Afaik Ubuntu doesn't have an equivalent to macOS' open. – Baptiste Candellier Oct 04 '18 at 15:02
  • 2
    @BaptisteCandellier I don't get the point of calling open to call another program. Why not just call the other program directly? What usefulness does open add? This answer reflects the natural thing to do to open specific applications. – JoL Oct 04 '18 at 15:35
  • @BaptisteCandellier I see your point, but the answer does what I wanted to do, so I accepted it. – miguelmorin Oct 04 '18 at 16:38
  • @mmorin you're the boss. :) – Baptiste Candellier Oct 04 '18 at 16:40
  • @JoL obviously this is the most Ubuntu-like answer and that's fine - however having a single command that can open anything as if you were just double-clicking the file in a file explorer can prove really useful too. – Baptiste Candellier Oct 04 '18 at 16:40
  • 1
    @BaptisteCandellier I agree, and open on macOS works both for opening files and folders in the default application and in specific applications. It seems the original bash went on a different path with xdg-open for files and the application names for the rest. – miguelmorin Oct 04 '18 at 17:00
  • 1
    @BaptisteCandellier https://askubuntu.com/a/15356 & – Dev Oct 04 '18 at 20:01
  • 1
    @BaptisteCandellier The equivalent to double-clicking is xdg-open. With xdg-open you don't specify a program name, just as you don't when double-clicking. – TRiG Oct 05 '18 at 00:28
  • For more thorough ways of launching a GUI app from the command line, see this question: How to cleanly launch a GUI app via the Terminal? My preferred method is ($app $arguments &>/dev/null &). – wjandrea Oct 05 '18 at 03:00
  • 2
    Another thing that is different about open in MacOS is that it launches the application detached from the console it's run from, i.e. if the console closes, the app will stay on. On Ubuntu one needs to use the nohup command to get the same behaviour. (since prefixing each command with nohup and redirecting output for each command you run gets tiring, it may make more sense to define a custom shortcut) – undercat Oct 05 '18 at 04:20
  • 1
    @undercat Personally I use a function: abandon()("$@" &>/dev/null &), then I can use it like abandon firefox http://example.org. – wjandrea Oct 05 '18 at 16:48
2

As indicated in a response to the accepted anwser, the equivalent to the open command from macOS in Linux is xdg-open:

xdg-open opens a file or URL in the user's preferred application. If a URL is provided the URL will be opened in the user's preferred web browser. If a file is provided the file will be opened in the preferred application for files of that type. xdg-open supports file, ftp, http and https URLs.

When compared to the macOS open, the main difference is that here you can't specify an specific application to use from the command line. For that, you can refer to the accepted answer, as the problem is kind of pointless in Linux because applications are normally available in your PATH already.

opsidao
  • 121
  • 2