Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Owner Author

MarcoDVisser commented Aug 26, 2014

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
You can’t perform that action at this time.