Linux equivalent of the Mac OS X “open” command [c

2019-01-12 13:38发布

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

The open command 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.

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

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

evince my.pdf

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

Thanks!

7条回答
孤傲高冷的网名
2楼-- · 2019-01-12 14:01

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:

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

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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-12 14:06

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

man see
查看更多
叛逆
4楼-- · 2019-01-12 14:07

gnome-open

查看更多
叼着烟拽天下
5楼-- · 2019-01-12 14:09

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

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.

Of course, you can alias it to open to 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 .bashrc file:

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:

] .

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

查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-12 14:20

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

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

查看更多
男人必须洒脱
7楼-- · 2019-01-12 14:26

Under Gnome Desktop environment, I use the following command:

nautilus `pwd` &

This is similar as "open ." command in Mac

查看更多
登录 后发表回答