Google Ads

February 22, 2017

Linux .desktop file run a executable file from the same directory

Desktop files in GNU/Linux define shortcuts for conveniently launching installed applications in the system from system menus and file managers. They can be found in many different places such as in /usr/share/applications directory for system wide applications and in ~/.local/share/applications for user applications. These files can be opened in any text editor program to edit application executable file names or for adding new options to application executables. A typical desktop file may look like below:

[Desktop Entry]

Name=Install
GenericName=Package Installer
Comment=Copy package files to your system
Exec=Install.sh
Terminal=false
Type=Application
Encoding=UTF-8

Each key and value pair defines different attributes of the application launcher. For example the Name key assigns a name for the application launcher which would appear in the system menus and file managers. The most important key is Exec which tells the name of the application executable file. The value of the Exec key can be either just the application executable file name or absolute address of the application executable file like shown below:


Exec=App.sh


Or

 
Exec=/opt/ApplicationX/App.sh

However, sometimes it is preferable to tell Exec key that the application executable file is located in the same directory as the desktop file. This can be easily achieved with the %k code which is available to the Exec key. The %k code in the Exec key value gives us the absolute location of the desktop file as either a URI (if for example gotten from the vfolder system) or a local filename or empty if no location is known. Now, to run a application executable in the same directory as desktop file, Exec key can have the following value:


Exec=xdg-open "%k"/App.sh


The above Exec key value also ensures portability across different GNU/Linux distributions and can be used for many different application scenarios such as shipping portable application packages.


That's all for today

Cheers!