How to open file with default application from command line?

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).


xdg-open and gnome-open

xdg-open is the most universal way (work also on KDE)


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
}

Tags:

Command Line