I am trying to find files with multiple extensions in a shell script

That's what i still have

DIR="/home/username/Pictures/Wallpapers"

while [ 1 -eq 1 ]
do
    PIC=$(ls $DIR/*.jpg | shuf -n1)
    PIC1="file://"$PIC

    gsettings set org.gnome.desktop.background picture-uri $PIC1
    sleep 30;
done

The script works, but I can’t figure out how to expand it to find other file extensions, like png or gif, in addition to jpg.

+5
source share
2 answers

How about using find?

find $DIR -name \*.jpg -o -name \*.png -o -name \*.gif -print
+12
source

Assuming bash, the simplest change would be

PIC=$(ls $DIR/*.{jpg,png,gif} | shuf -n1)

Here is a sample on my car

$ export DIR=Desktop
$ PIC=$(ls $DIR/*.{jpg,png,gif} | shuf -n1)
$ echo $PIC

Desktop/polar.png

Instead, I recommend using a search for this.

+3
source

All Articles