Random desktop background from command line

As mentioned in my previous post i synchronize my wallpaper folder between my desktop computers.

While the are various ways of setting a random desktop background (XFCE – which I use – has this built in), I’ve had to conclude it’s just easier from the command line.

In XFCE it is not possible to do this from cron it seems, so I made a script which is run at start-up:

#!/bin/bash

IFS='
'

DIR="$HOME/etc/wallpaper"



while true
 do
        PIC=$(ls $DIR/*.* | shuf -n1)
        # Uncomment based on desktop environment
    # XFCE:
    #/usr/bin/xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s ${PIC}
    #/usr/bin/xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-style -s 5
    # GNOME2
    #gconftool-2 --type=string --set /desktop/gnome/background/picture_filename ${PIC}
    #gconftool-2 --type=string --set /desktop/gnome/background/picture_options zoom
    # GNOME/Unity/Cinnamon
    #gsettings set org.gnome.desktop.background picture-uri file://${PIC}
    #gsettings set org.gnome.desktop.background picture-options zoom
    # Generic - uses feh
    #feh --bg-fill ${PIC}
        sleep 120
done

In order to make the script run at start-up, create the file $HOME/.config/autostart/wallpaper.desktop

[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=Rotate bg
Comment=
Exec=/home/alj/bin/setwallpaper.sh
StartupNotify=false
Terminal=false
Hidden=false

If you are not using a desktop environment like GNOME or XFCE, add the script to .xsession.

Leave a Reply

Your email address will not be published. Required fields are marked *