TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG:3183] select random file from directory



On Thu, 7 Jan 1999, Ben Luey wrote:

> What is the easiest way to select a random file from a bunch of files in
> one directory. I don't want to have to maintain a fortune database because

Here's the most difficult way.  This bash shell command line will give you
a random file name from the current directory:

ls | head -n$[$(ls | wc -l | gawk '{srand(); print rand() * $1}' \
| gawk -F. '{print $1}') + 1] | tail -n1

It will count the number of files, N, in the current directory, then
choose a random number between 1 and N, and display the corresponding file
name.

hope that helps,
Chris