Changing Your Linux Background Automatically - gHacks Tech News

Changing Your Linux Background Automatically

Compared to Windows and Apple, some people consider Linux distributions to be the most user-friendly, personally customizable operating systems available. They enable you to adjust and tweak pretty much anything you want to. All they require is a basic understanding of the dynamic of Linux.

One fun change you can make is customizing your desktop in a pretty unique way – by changing the background image every so often automatically. While some programs exist to help you with this task, there is absolutely no reason why you cannot do it yourself with a few simple commands. Before long you can have your entire vacation album scrolling behind your open windows, changing photos every five, ten, or thirty minutes – it's entirely up to you.

To begin, open a terminal window and open a new text file:

gksu gedit

You are going to insert a line of commands into this file, which you will later turn into a shell script. The function of each line is outlined below; you only need to copy and paste the big block of text further down. The individual explanations are so you can customize the code to fit your needs.

picsfolder=”/media/documents/photosfordesktop”

This line points to whatever folder holds the photos you want scrolling in the background. Be sure to change it to whatever folder holds yours.

cd $picsfolder

This changes the current directory to the new folder full of pictures.

files=(././.jpg)

This creates an array out of all your photos, allowing them to be mixed up for display later.

N=${#files[@]}

This simply identifies the number of pictures in the new array.

((N=RANDOM%N))

This chooses a random photo from those you made available.

randomfile=`echo ${files[$N]} | cut --characters=”1 2” --complement`

This identifies the name of the specific photo chosen.

Gconfigtool-2 -t str –set /desktop/gnome/background/picture_filename “$picsfolder$randomfile”

This sets the desktop with your new (temporary) photo as the background.

Now, take all those commands, put together below, and copy and paste them into the text editor you opened earlier:

picsfolder=”/media/documents/photosfordesktop”
cd $picsfolder
files=(././.jpg)
N=${#files[@]}
((N=RANDOM%N))
randomfile=`echo ${files[$N]} | cut --characters=”1 2” --compliment`
Gconfigtool-2 -t str –set /desktop/gnome/background/picture_filename “$picsfolder$randomfile”

Save your text document as /bin/timedwallpaper.sh and exit the text editor. Everything is ready – now you just need to tell your computer to do it. For this next part your distribution needs to have “cron” installed, a program which typically comes pre-packaged. To make sure you have it, type

sudo apt-get install cron

Once you have ensured it is installed, type

crontab -e

At the bottom start a new line and type

*/5 * * * * timedwallpaper.sh

This sets the time between pictures at five minutes. Change the number to whatever you see fit. Arrow down to the next blank line, and then type

@reboot timedwallpaper.sh

After that, press Control+x, then y, and finally enter. Exit the terminal and you are good to go. Not only will your script run however often you program it to, it will now reset each time you start your computer as well. Enjoy the scenery!

Advertisement

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:


Previous Post: «
Next Post: »

Comments

  1. jesus said on June 5, 2011 at 11:39 pm
    Reply

    just checking in to say that ubuntu unity sucks no matter what kind of wallpaper you put on it. It’s like putting lipstick on a pig.

    1. Jef said on June 6, 2011 at 12:27 pm
      Reply

      If you don’t like Unity, don’t use it…simple as that. I have Ubuntu 11.04 with Xfce4 as my DE. Try it, you might like it.

  2. Jose said on June 7, 2011 at 4:53 am
    Reply

    Shouldn’t that cut parameter be –complement instead of –compliment? :-)

  3. dpthakar said on June 7, 2011 at 2:39 pm
    Reply

    nice trick
    this is exactly what i was looking for few months back

    i found easy way to do it on gnome.
    http://www.linuxreaders.com/2010/10/25/ramdom-wallpaper-gnome/

  4. Ajay said on June 8, 2011 at 9:05 pm
    Reply

    I used dpthakars link and simplified the timedwallpaper.sh script to these 2 lines:

    /bin/cat ~/Documents/Wallpapers/`ls ~/Documents/Wallpapers/ |sort -R |tail -1` > ~/Documents/Wallpapers/MyWallpaper.jpg
    gconftool-2 -t string -s /desktop/gnome/background/picture_filename ~/Documents/Wallpapers/MyWallpaper.jpg

    Works like a charm!!

  5. NickNackGus said on June 21, 2011 at 3:32 pm
    Reply

    I made some modifications to my program:
    This looks at all images, instead of jpegs. I have to run it as to make it run outside of terminal. I also put an echo command in between to debug, but I left those out of this post. You can also modify it for other things, such as playing a random sound when Blue Proximity notices you are leaving or moving towards your computer. Right now mine is playing sound effects from Portal’s turrets.
    As for Unity, I have not yet upgraded to 11.04 (mainly because of all the complaints about Unity), and plan to run a live CD to test it. Testing it through a VM left it with so little video capabilities, it couldn’t load Unity, and defaulted to the previous…whatever part of the graphics it is. I’m not sure what it’s called, but if I don’t like Unity, I can at least uninstall it through Software Center or Synaptic without problems.

  6. NickNackGus said on June 21, 2011 at 3:34 pm
    Reply

    Sorry, my programs were in less/greater than signs, and treated as hidden. backgrounds program:
    picsfolder=”/home/tim/Public/Files/images/computer/Backgrounds”
    cd $picsfolder
    pwd
    files=(././*)
    N=${#files[@]}
    ((N=RANDOM%N))
    randomfile=${files[$N]}
    randomfile=cut –characters=5- $randomfile
    gconftool-2 -t str –set /desktop/gnome/background/picture_filename “$picsfolder/$randomfile”

    and to run, enter:
    bash ‘program.txt’

  7. NickNackGus said on December 26, 2011 at 2:14 am
    Reply

    Can anyone fix this for Oneiric? I got the program to run without throwing an exit code, but it isn’t changing the background either. My present code is as follows:

    picsfolder=”/home/tim/Public/Files/images/computer/Backgrounds”
    echo “pics are in” $picsfolder
    cd $picsfolder
    pwd
    echo “working in” $picsfolder
    files=(././*)
    N=${#files[@]}
    echo “found” $N “files”
    ((N=RANDOM%N))
    echo “picked file” $N
    randomfile=${files[$N]}
    randomfile=`echo $randomfile | cut –characters=5-`
    echo “file name is” $randomfile
    gconftool-2 -t str –set /desktop/gnome/background/picture_filename “$picsfolder/$randomfile”
    echo done

  8. Al said on October 19, 2017 at 2:13 pm
    Reply

    I find it easier to use feh, and this script in .xinitrc

    #!/bin/bash
    while true; do
    feh –randomize –bg-fill ~/backgrounds/*
    sleep 1m
    done

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.