If you can set the file with
gsettings set org.gnome.desktop.background picture-uri file:///path/to/the/file
then you can get the file with
gsettings get org.gnome.desktop.background picture-uri
That will give you a string like 'file:///usr/share/images/desktop-base/desktop-background.xml'
which is nice but needs to be parsed before we can directly use it as an argument to another shell command. So we can easily strip out the quotes and the file://
part with cut
, like:
gsettings get org.gnome.desktop.background picture-uri | cut -d/ -f3- | cut -d\' -f1
(Note: the returned file is actually an XML file for me, but I'm not using GNOME, so I don't know how this works on a live system. There may be another processing step that needs to happen here, before you can start comparing files.)
And since you already know the name of the file you want to compare against, you can use cmp
to compare those two files:
cmp my/file.jpg "$(gsettings get org.gnome.desktop.background picture-uri | cut -d/ -f3- | cut -d\' -f1)"
cmp
will set a return value of 0
(true), if the files are the same, or non-zero (false) if they're different or something went wrong. We can use that to decide what to do next, perhaps using the ||
operator which will run a command only if the previous command returned non-zero (false). We can also suppress the output from cmp
with the -s
switch. That gives us this command that you could, for example, run as a regular cron job:
cmp -s my/file.jpg "$(gsettings get org.gnome.desktop.background picture-uri | cut -d/ -f3- | cut -d\' -f1)" || gsettings set org.gnome.desktop.background picture-uri file:///full/path/to/my/file.jpg