Recurse directories and return random file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recurse directories and return random file
# 1  
Old 07-20-2018
[SOLVED] Recurse directories and return random file

I have a nice program to change the background but I want it to operate on subdirectories as well.

Code:
# Script to randomly set Background from files in a directory
while true;do

# Directory Containing Pictures
DIR="/home/pc/Pictures"

# Internal Field Separator set to newline, so file names with
# spaces do not break our script.
IFS='
'

# Command to Select a random jpg file from directory
PIC=$(ls $DIR/*.jpg | shuf -n1)

# Command to set Background Image
gsettings set org.mate.background picture-filename $PIC

# specify how long to wait in seconds between changes
sleep 600

done


Last edited by triplemaya; 07-21-2018 at 09:22 AM.. Reason: SOLVED
# 2  
Old 07-20-2018
Try
Code:
find $DIR -iname "*.jpg"

in lieu of the ls command.
# 3  
Old 07-20-2018
Hm. To prevent find from running too often - disk intensive to list an entire big directory tree -- how about:

Code:
DIR="/home/pc/Pictures"

while true
do
        find "$DIR" -type f -name '*.jpg' | shuf | while read PIC
        do
                gsettings set org.mate.background picture-filename "$PIC"
                sleep 600
        done
done

Instead of taking the first from the randomized list then throwing the rest away, it will use the entire random-order list.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 07-21-2018
Many thanks to you both. All working very nicely now.
This User Gave Thanks to triplemaya For This Post:
# 5  
Old 07-25-2018
If you have ImageMagick installed you could use it to identify those images that are too small for the backgroud (eg with a width < 1200 pixels)

Code:
IR="/home/pc/Pictures"

while true
do
        find "$DIR" -type f -name '*.jpg' | shuf | while read PIC
        do
                (($(identify -format "%w" "$PIC") < 1200)) && continue
                gsettings set org.mate.background picture-filename "$PIC"
                sleep 600
        done
done


Last edited by Chubler_XL; 07-25-2018 at 12:13 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Clear contents of specified directories, then return exit status

Hello, this is my first post here. I'm attempting to write a bash shell script to rm the contents of a directory without deleting the directory, specifically in OS X 10.10 . Here's what I have: function clear() { USER="$USER" DIR=$1 rm -rfv /Users/"$USER"/library/$DIR/* } clear... (6 Replies)
Discussion started by: YouNicks
6 Replies

2. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

3. Shell Programming and Scripting

Function - play # uses mplayer random directories

I have this in my .bashrc and use it to play my music. It is organized by directories and this will play them in random order: play(){ ## play directories with mplayer (random) if then # if no option show music types echo -n " play {directory|style} Styles: -------" find... (0 Replies)
Discussion started by: bdragon
0 Replies

4. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

5. Shell Programming and Scripting

Return all sub directories on ftp server

Hey guys, I'm new to the programming world in general, and I only write in applescript so far. I am trying to find a shell script that will return a list of all the sub directories (or folders) within a directory on my ftp server. The server needs a username and password. I would assume the... (4 Replies)
Discussion started by: thriftinkid
4 Replies

6. UNIX for Dummies Questions & Answers

Copy file into directories and sub-directories

Hello- I need to copy a file into multiple directories, and each directory's sub-directories (of which there are 5) Currently, the parent directory is set up like this: dir1 sub-dir1 sub-dir2 sub-dir3 sub-dir4 sub-dir5 dir2 sub-dir1 sub-dir2 sub-dir3 ... (1 Reply)
Discussion started by: penlok
1 Replies

7. Shell Programming and Scripting

how to change the current file processing to some other random file in awk ?

Hello, say suppose i am processing an file emp.dat the field of which are deptno empno empname etc now say suppose i want to change the file to emp.lst then how can i do it? Here i what i attempted but in vain BEGIN{ system("sort emp.dat > emp.lst") FILENAME="emp.lst" } { print... (2 Replies)
Discussion started by: salman4u
2 Replies

8. UNIX for Dummies Questions & Answers

Any way to grep a string in directories and return the result with diskusage aswell?

What Im basically trying to do is this: I have a small script that can grep any parameter entered into a search string, then print to the screen the name of each file the parameter appears in as well as the file path, ie the directory. The code Im using just for this is.... Directory... (3 Replies)
Discussion started by: Eddeh
3 Replies

9. UNIX for Dummies Questions & Answers

recurse through filesystem to delete files

howdy all -- I recently trashed my old PC and bought a MacBook. Moving all my files over, I find I've got a zillion instances of stupid Windows files like Thumbs.db and desktop.ini all over the place. I'm looking for a way to delete them all at once (deleting each manually would take forever),... (2 Replies)
Discussion started by: mdtreky
2 Replies

10. UNIX for Dummies Questions & Answers

scp transmits ok but random return code

Appreciate your thoughts....I m very new to this. Anyone here have the similar experience and work around. Thanks. Use scp to send a file from HP-UX to SUN box successfully but return code randomly being generated. The majority of time reports 1 (meaning not ok) and sometime 0 (OK). When scp... (0 Replies)
Discussion started by: huiraym
0 Replies
Login or Register to Ask a Question