如何从命令行打开带有默认应用程序的文件?

在Nautilus中,当您双击文件时,它将使用与文件扩展名关联的默认应用程序打开它。 例如,.html文件将在Web浏览器中打开,而.pdf将使用Document Viewer打开。 有没有办法在命令行中重现相同的行为(即open path/filename )? 我问,因为我喜欢从命令行浏览我的文件系统,但有时候不记得哪个应用程序。 打开什么。

我认为xdg-open是你要找的命令。

 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. 

例如: xdg-open index.php

这将在gedit中打开index.php(如果你使用的是gnome)。

如果你想在浏览器中打开一个url

 xdg-open http://google.com 

这将在您的默认浏览器中打开google.com。


xdg-open是一个包装脚本 – 它将使用桌面环境的工具( gio opengvfs-openkde-opengnome-opendde-openexo-open以及许多其他此类工具)。 它也是默认安装的,很可能适用于过去,当前和未来的版本(另一方面, gvfs-opengnome-open已被弃用,并且可能在将来的版本中不可用)。

xdg-opengnome-open

xdg-open是最通用的方式(也适用于KDE)

如果你想:

  • 为此命令创建别名(例如, open
  • 隐藏命令的输出
  • 之后继续使用此终端

你可以使用这个.bashrc函数 :

 function open () { xdg-open "$@">/dev/null 2>&1 }