In Nautilus, when you double click a file, it will open it with the default application associated with the file's extension. For instance, .html files will open in a web browser and .pdf will be opened with Document Viewer. Is there a way to reproduce the same behavior from within the command line (i.e. open path/filename)? I'm asking because I like to browse my file-system from the command line but sometimes don't remember which app. opens what.
Asked
Active
Viewed 1.6e+01k times
243
Radu Rădeanu
- 166,822
- 48
- 327
- 400
Olivier Lalonde
- 57,431
- 62
- 125
- 146
-
There is a similar question regarding launching default browser from the command line: http://askubuntu.com/questions/8252/how-to-launch-default-web-browser-from-the-terminal – kounryusui Nov 29 '10 at 13:42
-
That's a brilliant question indeed! In almost 4 years of my `bash`ing I hadn't thought about it :P – John Strood Jun 07 '16 at 09:40
3 Answers
314
I think xdg-open is the command you are looking for.
NAME
xdg-open - opens a file or URL in the user's preferred application
SYNOPSIS
xdg-open {file | URL}
xdg-open {--help | --manual | --version}
DESCRIPTION
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.
eg: xdg-open index.php
This will open index.php in gedit(if you are using gnome).
If you want to open a url in browser
xdg-open http://google.com
this will open google.com in your default browser.
xdg-open is a wrapper script - it will use the desktop environment's tool (gio open, gvfs-open, kde-open, gnome-open, dde-open, exo-open, and a host of other such tools). It is also installed by default, and very likely to work on past, current and future versions (on the other hand, gvfs-open and gnome-open have been deprecated, and may be unavailable in future releases).
-
2
-
-
7easy way to shorten this command is to use an alias. `alias o='xdg-open'`. Put this inside your `.bash_aliases` file to make the alias load on startup each time. `nano ~/.bash_aliases` and then paste inside `nano` using `CTRL+SHIFT+V`. – fIwJlxSzApHEZIl Sep 21 '17 at 15:44
-
1The command `browse` seems to do the same (seen on Ubuntu 19.04). – Cedric Reichenbach Nov 12 '19 at 16:29
-
2For other novices like me, `xdg` stands for [*X (cross) Dekstop Group*](https://www.reddit.com/r/linuxmasterrace/comments/jiizsm/what_does_xdg_mean/) – Jeremiah England Nov 20 '20 at 13:54
52
xdg-open and gnome-open
xdg-open is the most universal way (work also on KDE)
shellholic
- 5,622
- 1
- 24
- 16
-
for ubuntu 10.04 it seems not to be granted that gnome-open is installed (Command 'gnome-open' not found, but can be installed with: sudo apt install libgnome2-bin). xdg-open was installed (at least on my machine :) ) – grenix Sep 28 '22 at 09:57
19
If you want to:
- make an alias for this command (e.g.
open) - hide output from the command
- continue using this terminal after
You can use this .bashrc function:
function open () {
xdg-open "$@">/dev/null 2>&1
}
jessexknight
- 351
- 2
- 14
-
2Alternative is a simpler version that does not hide output or move to background: `alias o='xdg-open`. – berkes Mar 26 '21 at 08:00
-
1I would at least call the function "myopen" or something because it may conflict with /bin/open. Btw this is a potential trap for any alias (even "myopen" might already exist). – grenix Sep 28 '22 at 10:01