Automate and Personalize: Change Ubuntu Desktop Wallpaper via Command Line and Cron

Revitalizing your desktop environment on Ubuntu Linux goes beyond mere customization—it’s about infusing your workspace with a personal touch that inspires creativity and productivity. This comprehensive guide not only walks you through the steps to manually change your desktop background using command line and the GNOME desktop environment but also introduces an innovative approach to automate this process. By leveraging a simple bash script coupled with the power of cron, a time-based job scheduler found in Unix-like operating systems, you can ensure that your desktop background refreshes automatically, presenting you with a new, randomly selected image from your collection at set intervals. Whether you are a seasoned Linux user or new to the command line, this tutorial will empower you to transform your desktop into a dynamic and personalized space.

In this tutorial you will learn:

  • How to identify your current GNOME color scheme
  • How to change your desktop background based on the color scheme

 

Automate and Personalize: Change Ubuntu Desktop Wallpaper via Command Line and Cron
Automate and Personalize: Change Ubuntu Desktop Wallpaper via Command Line and Cron
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux with GNOME desktop environment
Software gsettings command-line tool, bash, cron
Other Path to the desired wallpaper image
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Changing Your Desktop Background on GNOME

Before diving into the steps, it’s worth noting why one might prefer using the command line over the graphical user interface (GUI) for such tasks. Command-line operations can be scripted and automated, making them ideal for users who enjoy creating a personalized computing experience or for system administrators managing multiple machines.

  1. Identify Your GNOME Color Scheme: Before setting a new wallpaper, you should check your current color scheme as it determines which setting to use for changing the wallpaper.
    $ gsettings get org.gnome.desktop.interface color-scheme

    This command will return either default or prefer-dark, indicating your current theme preference. This is crucial as GNOME provides different settings for each theme to better integrate your wallpaper with the system’s overall look.

    Identify Your GNOME Color Scheme
    Identify Your GNOME Color Scheme
  2. Changing the Desktop Background: Depending on your color scheme, use the appropriate command to change your wallpaper.
    For prefer-dark color scheme:

    $ gsettings set org.gnome.desktop.background picture-uri-dark file:///home/linuxconfig/wallpaper.jpg

    For default color scheme:

    $ gsettings set org.gnome.desktop.background picture-uri file:///home/linuxconfig/wallpaper.jpg

    Replace /home/linuxconfig/wallpaper.jpg with the full path to your desired wallpaper image. This command updates your desktop background without the need for logging out or restarting your session.

Automating Wallpaper Changes with a Bash Script

To further enhance the customization of your desktop environment, you can automate the process of changing your wallpaper by using a simple bash script. This script will randomly select an image from a specified directory /home/linuxconfig/Pictures/ and set it as your desktop background. This automation brings a dynamic aspect to your desktop, keeping the visual experience fresh and exciting.

The script provided below should be saved to a file (for example, change-wallpaper.sh) and given executable permissions. Here’s a step-by-step guide on creating and executing the script:

  1. Creating the Script: Copy the bash script provided below into a new file named change-wallpaper.sh within your home directory or another convenient location. Please remember to update the WALLPAPER_DIR variable in the script with the path to your own directory where your wallpapers are stored, ensuring the script selects from your personal collection of images.
    #!/bin/bash
    
    # Define the directory containing the wallpapers
    WALLPAPER_DIR="/home/linuxconfig/Pictures/"
    
    # Make sure we can connect to the desktop environment
    export DISPLAY=":0"
    export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus"
    
    # Select a random wallpaper from the directory
    SELECTED_WALLPAPER=$(find "$WALLPAPER_DIR" -type f | shuf -n 1)
    
    # Determine the current color scheme to decide which property to set
    COLOR_SCHEME=$(gsettings get org.gnome.desktop.interface color-scheme)
    
    # Change the wallpaper based on the color scheme
    if [ "$COLOR_SCHEME" = "'prefer-dark'" ]; then
        # Set the wallpaper for dark theme
        gsettings set org.gnome.desktop.background picture-uri-dark "file://$SELECTED_WALLPAPER"
    else
        # Set the wallpaper for light theme
        gsettings set org.gnome.desktop.background picture-uri "file://$SELECTED_WALLPAPER"
    fi
    
    echo "Wallpaper changed to $SELECTED_WALLPAPER"
  2. Make the Script Executable: Open a terminal and navigate to the directory containing your script. Run the following command to make it executable:
    $ chmod +x change-wallpaper.sh
  3. Executing the Script: To manually change the wallpaper, simply execute the script by running:
    $ ./change-wallpaper.sh

    in the terminal.



Setting Up an Automated Wallpaper Change Using Cron

To have your wallpaper change automatically every hour, you can use cron, a time-based job scheduler in Unix-like operating systems. Here’s how to set up a cron job for this task:

  1. Open your crontab file for editing by running:
    $ crontab -e

    in the terminal.

  2. Add the following line to the end of the file, adjusting the path to where your script is saved:
    0 * * * * /path/to/your/change-wallpaper.sh

    This cron job will execute the script at the start of every hour, changing your desktop background to a random image from the specified directory.

    Setting Up an Automated Wallpaper Change Using Cron
    Setting Up an Automated Wallpaper Change Using Cron
  3. Save and close the file. The cron job is now set up and will run according to the schedule you’ve defined.

By following these steps, you can enjoy a constantly refreshing desktop environment without any manual intervention, making your Ubuntu experience more dynamic and personalized.

Conclusion

In conclusion, this guide has provided you with a comprehensive approach to personalizing your Ubuntu desktop experience. By mastering the command line technique to change your desktop wallpaper and implementing a bash script for automatic, random selections, you’ve taken a significant step towards creating a dynamic and stimulating workspace. The addition of a cron job to schedule these changes further enhances your desktop, ensuring it remains fresh and reflective of your personal style. Whether for productivity, inspiration, or simply the joy of customization, the skills you’ve acquired today promise a more engaging and personalized computing environment. Embrace these techniques to keep your desktop lively and uniquely yours.