Single Command to Edit .desktop file in Ubuntu & other Linux

 

This simple tutorial shows how to edit .desktop files, the config files for your apps shown in start menu (app launcher), in Linux via single command.

In most Linux, the app icons (and their names) you see in dock & launcher are handled by .desktop files. If need, user can edit them by using either text editor or even third-party apps.

For software developing, scripting, or editing multiple .desktop files at the same time, there’s a command line tool that can help!

What you can do by editing .desktop file for your app:

The .desktop files are usually stored in following locations:

  • /usr/share/applications – for system wide.
  • $HOME/.local/share/applications – for current user only.
  • /var/lib/flatpak/exports/share/applications – for Flatpak apps (system wide).
  • $HOME/.local/share/flatpak/exports/share/applications – for Flatpak apps (current user).
  • /var/lib/snapd/desktop/applications/ – for Snap apps.

By editing a .desktop file that is associated with your app can do following things:

  • Change app icon
  • Change the app name in start menu.
  • Hide app
  • Group app icon in different categories.
  • Associate with different file types.
  • And more.

Find out the .desktop file for your app

If you don’t know where is the .desktop file, then try following steps to find it out.

First, open terminal (Ctrl+Alt+T) and run command to install plocate (or mlocate for old Ubuntu):

sudo apt install plocate

Then, update the database by running command:

sudo updatedb

Finally, try searching the desktop file for your apps (Firefox for example):

locate "*firefox*desktop"

In last command replace firefox with the keyword for yours (case sensitive). And, copy the path-to-file for the one in the location mentioned above.

Single command to edit the .desktop file

As far as I know, Debian, Ubuntu, Fedora, Manjaro, and their based systems have desktop-file-edit tool out-of-the-box for edit .desktop entries.

Option 1: To change app name, use command:

desktop-file-edit --set-name=NEW_NAME /path/to/file.desktop

In command, sudo is required for .desktop file in system directories.

For example, change the pre-installed Firefox (Snap version) in Ubuntu to “My Web Browser”:

sudo desktop-file-edit --set-name="My Web Browser" /var/lib/snapd/desktop/applications/firefox_firefox.desktop

Option 2: To change app icon, use command:

desktop-file-edit --set-icon=/path/to/icon /path/to/file.desktop

For icon file under /usr/share/icons, or .local/share/icons, just replace /path/to/icon-file with file-name without extension (e.g., .png, .svg).

For example, change the icon for my Firefox browser:

sudo desktop-file-edit --set-icon='/home/ji/Pictures/icons/myfirefoxicon.png' /var/lib/snapd/desktop/applications/firefox_firefox.desktop

Option 3: To add/remove category or mime type, use command:

desktop-file-edit --add-category=VALUE_HERE /path/to/file.desktop

Replace --add-category with one of the options below depends on which action you want to do:

  • --remove-category
  • --add-mime-type
  • --remove-mime-type

See list of well known categories.. And, right-click on a file and go to its ‘Properties’ to check “Type”.

Option 4: To add/edit other keys.

Use “–set-key=KEY_NAME –set-value=KEY_VALUE” option. You can add more than one pair of the options in single command.

For example, hide Firefox from app launcher by setting “NoDisplay=true“. It will add the key ‘NoDisplay’, if not exist.

sudo desktop-file-edit --set-key=NoDisplay --set-value=true /var/lib/snapd/desktop/applications/firefox_firefox.desktop

Option 5: To remove a key, use “–remove-key=KEY_NAME” option.

For example, un-hide Firefox by deleting “NoDisplay=true” from the .desktop file:

sudo desktop-file-edit --remove-key=NoDisplay /var/lib/snapd/desktop/applications/firefox_firefox.desktop

Option 6: Edit multiple desktop files

You can edit more than one files using for loop.

For example, hide all the Waydroid Android app icons associated with the .desktop files under .local/share/applications directory.

for file in $Home/.local/share/applications/waydroid.*.desktop; do desktop-file-edit --set-key=NoDisplay --set-value=true $file; done

In addition, the desktop-file-edit command will automatically validate the .desktop file after done editing it. It will output errors if validation not passed. For more, see its man page.