Linux 等效于 Mac OS X“打开”命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/264395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:41:39  来源:igfitidea点击:

Linux equivalent of the Mac OS X "open" command

linuxmacoscommand-linecommand-line-interface

提问by

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

我发现 Mac OS X 中的“打开”命令在命令行中非常方便。来自“人开”:

The opencommand 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.

open命令会打开一个文件(或目录或 URL),就像您双击了该文件的图标一样。如果未指定应用程序名称,则使用通过 LaunchServices 确定的默认应用程序打开指定的文件。

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

也就是说,如果我想用默认的 PDF 查看器(恰好是预览)打开一个 PDF 文件,我只需要这样做:

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

然而,在 Linux 中,要从命令行打开 PDF 文件,我不得不四处寻找默认的 PDF 查看器,例如,“evince”(谁会猜到??),然后

evince my.pdf

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

那么,在 Linux 命令行中是否有一个简单的“open”命令等价物?

Thanks!

谢谢!

采纳答案by number5

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

你可以试试xdg-open,大多数 Linux 发行版都有。它将为您的文件打开默认关联的应用程序。

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

仅供参考https://portland.freedesktop.org/doc/xdg-open.html

回答by Ana Betts

gnome-open

侏儒开放

回答by Jeremy Visser

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

您正在寻找的等效项是xdg-open,它的使用方式与 OS X 的open命令相同。例如:

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.

然而,这真的很难快速准确地输入。相反,您应该为 xdg-open 创建一个别名,这会使该过程更快。

Of course, you can alias it to opento 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 .bashrcfile:

当然,您可以将其别名为open以使其与 OS X 匹配(您可以选择任何您想要的),但就我个人而言,]出于速度原因,我使用正确的方括号 ( ) 作为我的快捷方式。要使用它,请将以下内容添加到您的.bashrc文件中:

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:

这还允许您在当前目录中打开文件浏览器(例如 Nautilus):

] .

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

根据经验,我发现单字母别名最适合上述快捷方式。毕竟,目标是效率。你可以返回并在 OS X 上创建相同的别名——我把它留给读者作为练习。:-)

回答by Ali Afshar

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

传统上,您可以使用“see”命令。只使用 run-mailcap。这将在没有 Gnome 和 X 等的情况下工作。

man see

回答by Anake

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:

我只是自己解决了这个问题,所以我想我会写下我是如何做到的,这与 Suan 的要求特别相关。这些步骤允许您只输入“打开”,而不是您不需要的消息覆盖您的终端:

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

创建一个名为openin的脚本~/bin,内容只是:

xdg-open "" &> /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.

保存并关闭脚本,然后键入“source .profile”(或 .bash_profile 如果相关)。就是这样,所以输入“打开音乐”将在 nautilus GUI 中打开您的音乐文件夹,并且不应在您的终端上输入任何内容。

回答by WonderLi

Under Gnome Desktop environment, I use the following command:

在 Gnome 桌面环境下,我使用以下命令:

nautilus `pwd` &

This is similar as "open ." command in Mac

这类似于“打开”。Mac 中的命令

回答by stacksia

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

如果您的 mime 设置正确,则可以使用mimeopen. 查看它的手册页,因为它有一些有用的选项。