用 xdg-open 从终端打开文件和链接

用默认应用打开任意文件或链接——相当于 macOS open 命令的 Linux 通用版。

Linux桌面xdg-open 打开文件效率低风险
执行 xdg-open ~/report.pdf 用默认查看器打开 PDF,执行 xdg-open . 用文件管理器打开当前目录,执行 xdg-open https://github.com 打开浏览器。

01命令

命令
$ xdg-open ~/report.pdf
xdg-open https://github.com
xdg-open .

02作用说明

xdg-open 会查询桌面环境的 MIME 数据库,选择与双击一致的应用,在 GNOME、KDE、XFCE 乃至配置过的 WSL 下都能工作。它是让 shell 脚本交接给 GUI 的可移植方式:报告脚本最后一句 xdg-open report.html 即可在浏览器中弹出结果。目录会在文件管理器中打开,因此 xdg-open . 是快速“打开这个文件夹”的方法。

03风险

风险低风险
使用条件xdg-utils (sudo apt install xdg-utils); requires a desktop environment on the same session
!注意: 在无头服务器(没有 DISPLAY/WAYLAND_SESSION)上 xdg-open 会失败或行为异常——它必须在有运行中桌面会话的机器上调用。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • xdg-open(1) — Linux man page
    Ubuntu Manpages · technical · 2026-09-17
    依据: xdg-open man page: 'opens a file or URL in the user's preferred application' noting it works for files, directories and http(s) URLs.

最后验证: 2026-09-17 · 分类: 开发者