TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [TCLUG:3183] select random file from directory
- To: tclug-list@listserv.real-time.com
- Subject: Re: [TCLUG:3183] select random file from directory
- From: Chris Kesler <chris@pconline.com>
- Date: Fri, 8 Jan 1999 09:01:41 -0600 (CST)
- In-Reply-To: <Pine.LNX.4.04.9901071600270.12525-100000@pcLueyB.Res.Carleton.edu>
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