「DESKTOP-ENTRY」- 桌面文件 | .desktop | Desktop Entry Specification

认识

文档:Desktop Entry Specification

.desktop 文件是一个遵循 freedesktop.org 标准的纯文本文件,其核心作用是为应用程序提供一个标准化的启动方式和在图形界面中的展示形式。简单来说,你可以把它理解成 Windows 系统中的“快捷方式”或 macOS 中的“替身”。当你在 Linux 桌面环境的应用程序菜单、启动器或桌面上看到一个程序图标时,背后通常都有一个 .desktop 文件在定义这个图标的行为。

组成

Exec=

搜索路径: The executable program can either be specified with its full path or with the name of the executable only. If no full path is provided the executable is looked up in the $PATH used by the desktop environment.

在 Exec= 中,使用复杂的脚本:Exec=sh -c ‘env “LD_LIBRARY_PATH=$HOME/.SynologyDrive/SynologyDrive.app/lib/” “$HOME/.SynologyDrive/SynologyDrive.app/bin/cloud-drive-ui” –open-file “‘%f'”‘

Q:在 Exec= 中,如果包含空格该如何处理?
A:Exec=”/opt/Xlash for Windows-0.20.37-x64-linux/xfw” “–no-sandbox”
R:在 GNOME .desktop 文件的 Exec 中,如果包含空格,应该如何填写?—— ChatGPT 4.0

性质

作用:
1)文件 .desktop 提供有关菜单中某个项的数据,如其名称、要运行的命令、图标。.desktop 文件还包含了用于确定其在菜单中位置的关键字。
2)文件 ~/.local/share/applications/mimeinfo.cache 包含 MIME 类型信息,这些类型信息将 $HOME/.local/share/applications/*.desktop 应用程序.desktop 文件中指定的文件类型 相关联。

Lines beginning with a # and blank lines are considered comments and will be ignored
Desktop Entry Specification

构建

desktop-file-utils

https://www.freedesktop.org/wiki/Software/desktop-file-utils

/usr/bin/desktop-file-edit
/usr/bin/desktop-file-install
/usr/bin/desktop-file-validate

  • 通过 desktop-file-validate 命令,我们能够验证 .desktop 文件是否有效或存在语法错误。

/usr/bin/update-desktop-database

apt-get install desktop-file-utils # Working on Ubuntu 25

所在目录

系统级:

  • /usr/share/applications – 系统安装的应用程序存放 .desktop 文件的标准位置。由软件包管理器(如 apt, dnf, pacman)管理。
  • /usr/local/share/applications/ – 用于本地编译安装的软件(较少见)。

用户级:

  • ~/.local/share/applications – 这是最推荐用户自行修改和创建的位置。

桌面级:

  • 某些桌面环境(如 KDE, XFCE)也会读取 ~/Desktop/(即用户的桌面文件夹)下的 .desktop 文件,并将其显示为桌面图标。

并且可以将应用程序添加到右键单击文件时出现的“Open With >”的子菜单中。

参考

DeepSeek / 介绍 Linux 中的 .desktop 文件
https://developer.gnome.org/desktop-entry-spec/