//View Tip #826
Similar Tips
» Remove DS_Store files from shared folders
» Find last modified files on a filesystem
» Delete old files
» Count files by type
» Replace expr by other recursively

 

Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
alias findrandom="find -type f -exec bash -c 'test \`echo \$((RANDOM%2))\` -eq 0' \; -print"

will give you a randomized find. Each file in all subdirs will be displayed with equal probability of 0.5.


View Comments »




Comments 

Add your comment

Comments are currently disabled
Similar command people might find useful:

find . | sort -R

sort -R requires coreutils 6 or later which isn't in RHEL 5 yet even though Sabayon ships with coreutils 7.4. So an alternative is:

find . | awk 'BEGIN {srand()} {print int(rand()*1000000) "\t" $0}' | sort -n | cut -f 2-

That's really hard to remember and/or type so if you have rl installed you can do:

find . | rl

I often use this to play a random file from my media directory:

find . -type f | rl | xargs -d "\n" mplayer
Posted 2009-06-04 16:32:13
gnu
Great, did not know of "sort -R". So a very fast alternative is

alias findrandom="find -type f | sort -R | sed 'n;d'"

("sed 'n;d'" gives you every second line)

Ok, it is not truly random, but this effect becomes irrelevant for a great number of files. Who needs true randomness anyway?
Posted 2009-06-07 10:28:38

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit