Random Backgrounds Script

I recently wrote this simple script to allow me to set a random image as my background:

#!/bin/bash
# Selects a random jpeg file in the ~/backgrounds/ directory (or it's
# sub-directories) and makes it the background image.
file_list=$(find ~/backgrounds/ -name "*.jpg")
num_files=$(echo $file_list | wc -w)
ran_line_number=$(($RANDOM%$num_files+1))
selected_file=$(echo $file_list | tr " " "\n" | sed -n ${ran_line_number}p)
fbsetbg $selected_file

On most light-weight window managers, like Blackbox, simply copy any images you want to use as backgrounds into ~/backgrounds/ and you're good to go. Some other Window Managers/Desktop environments look after the desktop background themselves so you would have to disable that in their configuration first. If you wanted to run this every half hour or something you could put it in your crontab, or else you can just set it up so it runs when you start your window manager.

-- Edit 09/05/2009 --

I've modified the code very slightly, to fix a bug which either didn't exist when run with the older version of bash/find I was have been using when I wrote this. I've also cleaned up the code a little.

Categories: Linux, Computers, Scripting
Date: 2008-08-06 23:03:54, 9 years and 201 days ago

Leave reply

No html allowed in reply

Notify me of follow-up comments via email.