Skip to content

Instantly share code, notes, and snippets.

@MarcoDVisser
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcoDVisser/bc36e6923e82b2626dc2 to your computer and use it in GitHub Desktop.
Save MarcoDVisser/bc36e6923e82b2626dc2 to your computer and use it in GitHub Desktop.
Random File Rename for random wallpaper in Linux
#!/bin/bash
# Seed random generator
RANDOM=$$$(date +%s)
# read all jpg files in folder
# replace the $( ) with '' for older versions
# creates an array of all the files in ~/bin/images/wp
FILES=(~/bin/images/wp/*)
NFILES="${#FILES[@]}" #determines the length of the FILES array
randomid=$((RANDOM % NFILES)) #$RANDOM selects a random number between 0 and $NFILES
F="${FILES[$randomid]}" #the random file
echo "Processing $F ..."
cp "$F" ~/bin/images/wp/tmp.jpg #does the actual moving
exit 0
@Dasonk
Copy link

Dasonk commented Aug 26, 2014

You don't need to muck around with using the date to generate random numbers in BASH - http://tldp.org/LDP/abs/html/randomvar.html

@Dasonk
Copy link

Dasonk commented Aug 26, 2014

And if you want to avoid having to explicitly set the wallpaper to tmp.jpg you could look here: http://stackoverflow.com/questions/5550895/shell-script-changing-desktop-wallpaper

@MarcoDVisser
Copy link
Author

Thanks, Dason. The script was more about teaching myself more about BASH scripting than anything else. This generic code works on all platforms, in a personal script I set my wp specific to the machine I'm on. In this case it's a machine running i3, and I use something like the above. However setting tmp.jpg as your back ground in works on all systems right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment