Linux: Changing CLI App to Desktop App

Creating a Desktop File for a CLI Executable

There are both desktop apps and terminal (CLI) apps in Linux. CLI apps do not get listed alongside the desktop apps in your app menu. Below I will step you through creating a Desktop Entry file. When this file is put in the correct folder, you can start the CLI apps just like desktop Apps.

I will make a Desktop Entry file for two CLI programs: Htop and Neofetch. Neofetch will require a bit more work because it quits after it’s output, so we will have to force it to wait for you to read its output.

I also created a video to show you here:


Some backgound

Desktop Entry files are the way that most Linux Desktop Distros are able to find list the programs that have been installed on you system. The are created to a standard set by https://freedesktop.org. Desktop Entry files have a .desktop file extension, and my be in certain folders for the linux to find them to list in menus and in app lists. You will find most of the Application type files listed in `\usr\share\applications' on your system.

Here is a simpler example of one from that folder for

/usr/share/applications/backintime-qt.desktop:

[Desktop Entry]
Name=Back In Time
GenericName=Backup
Exec=backintime-qt
Icon=document-save
Terminal=false
X-MultipleArgs=false
Type=Application
StartupNotify=true
Categories=Qt;KDE;GNOME;System;
Comment=Simple backup system
Comment[sl]=Enostaven sistem ustvarjanja varnostnih kopij
Comment[de]=Ein simples Backup-Programm inspiriert von »Time Machine«, »TimeVault« und dem »flyback project«.
Keywords=automatic;snapshot;restore;rsync;

The shell variable $XDG_DATA_DIRS list all the folders that the system searches for applications directories that would have the .desktop files. You can see this list by issuing echo $XDG_DATA_DIRS in a terminal window.

A good reference to read more, is to read the wiki.archlinux.org article Desktop entries

Creating Desktop Entry for htop

Note: This example shows using htop, if you don’t have htop installed, you could try any command that expects user interaction, such as top or nano.

Open a terminal and do the following commands to create a desktop entry file for htop.

Note: you can make your app available to all users if you put the desktop entry file into /usr/bin, but you should be familiar with working as root with sudo and the dangers of this.

Add these lines to htop.desktop in your editor. (substitute nano for htop is you are doing that app)

[Desktop Entry]
Type=Application
Terminal=true
Name=HTop
Exec=htop

Four entries is the minimum number of lines for a Desktop Entry file to make it work. I have tested this in five desktop systems: Gnome, KDE, Mate, XFCE, and Cinnamon. It should work in any desktop system.

  • The Name parameter is the name the app will appear in menus as.
  • The Exec is the command you would type in a terminal to run it or a path to the executable.

The optional other parameters you might add are

icon=path/To/Icon
Categories=Administration;Utility
  • The Icon parameter would be the filename or path to an icon to associate the app with.
  • The Categories entry suggest to the OS what categories the app will be listed in.

You can look at your application menu now, and it should show up in misc, or other.

Note: some distros you may have to log off and back in to have the new desktop entry file show up in menus

App Entry for neofetch (a information output command)

Note: This example shows using neofetch, if you don’t have neofetch installed, you could try any command that expects user interaction, such as df or free.

Terminal CLI commands that only output information, such as df or neofetch won’t work with the previous approach. Since the command will close as soon as it has output its text.

To fix this, we are going to write a simple shell script to call the command and then to sleep the terminal for a while to allow you to read the output.

Next, we will be creating a script in your user bin folder. Type the following commands in the terminal:

mkdir ~/bin
cd ~/bin
nano neofetch.sh

and then add this content in the editor:

neofetch
sleep 30m

next while you are in the bin directory type

sudo chmod +x neofetch.sh

This makes the script executable

Now go to your local applications folder:

cd ~/.local/share/applications
nano neofetch.desktop

and add this content and save:

[Desktop Entry]
Type=Application
Terminal=true
Name=NeoFetch
Exec=/home/username/bin/neofetch.sh

Try it out now. The 30m sleep will cause the app to close in 30m. You can also just close it by closing the terminal window, or type ctrl+c.

Note: some distros you may have to log off and back in to have the new desktop entry file show up in menus

I hope you learned something by this little trip into the inner workings of Linux.

Thank you for reading.