Set a random wallpaper at startup in KDE Plasma
I’ve been a Openbox & Xfce user most of my GNU/Linux life. Only recently, I’ve started using KDE Plasma. In Openbox one can easily run a command via its autostart
file to set a random wallpaper,
feh --no-fehbg --bg-scale --randomize path/to/the/wallpaper/directory/* &
Xfce makes it even easier as there already is an option to set a random wallpaper at startup in its settings.
In KDE Plasma, I was unable to find any option to set a random wallpaper at the session startup so I came up with a little hack to achieve this.
I keep all my wallpapers in $HOME/Pictures/Backgrounds
directory and this hack is hard-coded to this particular path so if you keep your wallpapers in some other location, you’ll have to change this path accordingly. Any location is valid as long as you have write permissions for that location.
First thing that we need to do is create a hidden directory .default-background
in $HOME/Pictures/Backgrounds
directory. To do so, open your favorite terminal emulator and run,
mkdir $HOME/Pictures/Backgrounds/.default-background
Next, we need to create a link
with the name of default_background.jpeg
in this hidden directory to any wallpaper image file in the wallpaper directory. For this example I’m gonna pick a wallpaper image called there-is-no-cloud.jpeg
,
ln -Pfn $HOME/Pictures/Backgrounds/there-is-no-cloud.jpeg $HOME/Pictures/Backgrounds/.default-background/default_background.jpeg
Now, right click on KDE Plasma desktop and select Configure Desktop and Wallpaper...
. Next, under the Wallpaper
tab, click on Add Image
option and select the link, default_background.jpeg
, that we have just created by navigating to the $HOME/Pictures/Backgrounds/.default-background
directory. After adding this, select it and hit the Apply
button to set it as the desktop wallpaper.
For our final step we need to create an empty file called random_background.sh
in /etc/X11/xinit/xinitrc.d
directory and make it executable,
sudo touch /etc/X11/xinit/xinitrc.d/random_background.sh
sudo chmod 755 /etc/X11/xinit/xinitrc.d/random_background.sh
Open this empty file as root or with sudo permissions in your favorite text-editor and paste the following code snippet into it,
#!/bin/sh
Backgrounds_Source="$HOME/Pictures/Backgrounds/"
Random_Background=$(find "$Backgrounds_Source" -type f -print0 | xargs -0 file --mime-type | grep -F 'image/' | cut -d ':' -f 1 | sort -R | head -n 1)
Background_Image="$HOME/Pictures/Backgrounds/.default-background/default_background.jpeg"
if [ -d "$Backgrounds_Source" ]; then
if [ -n "$Random_Background" ]; then
ln -Pfn "$Random_Background" "$Background_Image"
fi
fi
Save the changes to the file.
That’s it! From now on, every time you log-in to a new session you’ll be greeted by a new wallpaper.
On certain GNU/Linux distributions, the directory /etc/X11/xinit/xinitrc.d
might not be present. In such a scenario, you’ll need to create random_background.sh
file in the /usr/local/bin
directory and after pasting the above mentioned code into it, make it executable as described earlier. After doing that, you’ll need to create random_background.desktop
file in either ~/.config/autostart
directory or /etc/xdg/autostart
directory and add the following snippet to it,
[Desktop Entry]
Type=Application
Name=Random_Wallpaper
TryExec=/usr/local/bin/random_background.sh
Exec=/usr/local/bin/random_background.sh
StartupNotify=false
NoDisplay=true
After saving the file, you’ll need to enable/add Random_Wallpaper
to your desktop session startup list. The procedure varies between different desktop environments but here is how you can navigate to autostart
setting on a few popular desktop environments,
KDE => System Settings > Startup and Shutdown > Autostart
Xfce => Menu > Settings > Settings Manager > Session and Startup > Application Autostart
LXQt => Menu > Preferences > LXQt Settings > Session Settings
Just in case, someone was curious about that there-is-no-cloud.jpeg
wallpaper,
Don’t hesitate to contact me if you have any question or confusion regarding this article or anything else on the blog.