#!/usr/bin/ruby # randfile [TOPLEVEL] - print a random existing file name path = ARGV[0] || "." while File.directory?(path) files = Dir.entries(path) - [".", ".."] break if files.empty? path = File.join(path, files.sample) end puts path