Skip to content

Instantly share code, notes, and snippets.

@ddksr
Created September 11, 2014 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddksr/34c44bc1302d238465f1 to your computer and use it in GitHub Desktop.
Save ddksr/34c44bc1302d238465f1 to your computer and use it in GitHub Desktop.
Recursive random file
#!/bin/bash
dir="directory" # no slash at end
files=$(ls -R $dir | awk '
/:$/&&f{s=$0;f=0}
/:$/&&!f{sub(/:$/,"");s=$0;f=1;next}
NF&&f{ print s"/"$0 }');
list=();
while read -r line; do
list[${#list[@]}]=$line;
done <<< "$files"
file="${list[RANDOM % ${#list[@]}]}"
while [[ -d $file ]]; do
file="${list[RANDOM % ${#list[@]}]}"
done;
kde-open "./$file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment