summaryrefslogtreecommitdiff
path: root/randfile.sh
blob: ec7c38a99362557b625ef30ff7f31b3e5e2b2985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

#select a random file recursively from the given folder

dir="."
if [[ $1 ]]; then
    dir=$1
fi

#recursively list all files
find ${dir} > /tmp/randfilefind

#get number of files
n=`wc -l /tmp/randfilefind | cut -d' ' -f 1`

#select a random file
i=`shuf -i 0-${n} -n 1`
f=`sed -n ${i}'p' /tmp/randfilefind`

echo "The Chosen File: "${f}

rm /tmp/randfilefind