How to automatically switch between dark mode and light mode in Linux Mint and Ubuntu

In this article we will learn how to switch automatically between Dark and Light themes in Linux Mint and Ubuntu. You will find all the steps and details in order to customize this solution.

Automatic Dark/Light Mode

First we need to create a script which is going to change the theme. For example:

  1. create new file:
sudo nano /home/user/Scripts/Theme/auto_change_new.sh
  1. Add this content(for Cinnamon, for gnome check below):
#!/bin/bash
echo export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS > lightscript.sh
echo export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS > darkscript.sh
echo "gsettings set org.cinnamon.desktop.interface gtk-theme Mint-Y-Sand" >> lightscript.sh
echo "gsettings set org.cinnamon.desktop.interface gtk-theme Mint-L-Dark-Red" >> darkscript.sh
chmod 755 lightscript.sh
chmod 755 darkscript.sh

currenttime=$(date +%H:%M)
if [[ "$currenttime" > "21:00" ]] || [[ "$currenttime" < "06:00" ]]; then
  ./darkscript.sh
else
  ./lightscript.sh
fi
  1. make the file executable:
chmod +x /home/user/Scripts/Theme/auto_change_new.sh
  1. Schedule execution by crontab job:
crontab -e

Add this to your crontab file:

0 8 * * * /home/user/Scripts/Theme/lightscript.sh
0 18 * * * /home/user/Scripts/Theme/darkscript.sh
@reboot /home/user/Scripts/Theme/auto_change_new.sh

You can customize the change based on your needs.

lightscript.sh

You can also use two scripts which to be scheduled for a given time period. Example of mixed light theme from new Linux Mint - lightscript.sh:

export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
gsettings set org.cinnamon.desktop.interface gtk-theme Mint-Y-Blue
gsettings set org.cinnamon.desktop.interface icon-theme Mint-Y-Navy
gsettings set org.cinnamon.theme name Mint-Y-Blue

We can create new script with code from the above and then schedule on daytime - running each hour.

darkscript.sh

For dark themes we can use similar script: darkscript.sh

export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
gsettings set org.cinnamon.desktop.interface gtk-theme Mint-Y-Dark-Blue
gsettings set org.cinnamon.desktop.interface icon-theme Mint-Y-Navy
gsettings set org.cinnamon.theme name Mint-Y-Dark-Blue

Schedule scripts

To schedule light and dark script we can use the following command:

crontab -e

and add two new lines for:

0 18,19,20 * * * /home/user/Shortcuts/ScriptS/Theme/darkscript.sh
0 7-12 * * * /home/user/Shortcuts/ScriptS/Theme/lightscript.sh

Running:

  • dark theme at 18:00, 19:00, 20:00
  • light theme from 07:00 to 12:00

Set new theme

To set new theme via terminal we can use:

gsettings set org.gnome.desktop.interface gtk-theme Mint-L-Dark-Red
gsettings set org.gnome.desktop.interface gtk-theme Mint-Y-Sand

Reset theme options

To set new theme via terminal we can use:

gsettings reset org.cinnamon.theme name
gsettings reset org.cinnamon.desktop.interface icon-theme
gsettings reset org.cinnamon.desktop.interface cursor-theme
gsettings reset org.cinnamon.desktop.interface gtk-theme

Theme options

There are 4 different options which can be set. To reset or set their values we can use the following commands.

Desktop theme

gsettings reset org.cinnamon.theme name

Icon Theme

gsettings reset org.cinnamon.desktop.interface icon-theme

Mouse pointer:

gsettings reset org.cinnamon.desktop.interface cursor-theme

Applications

gsettings reset org.cinnamon.desktop.interface gtk-theme

Depending on the system we can use:

  • cinnamon - org.cinnamon.desktop...
  • gnome - org.gnome.desktop...

Below you can find the UI for themes in Linux Mint cinnamon:

Resources