Is there a bash command for picking a random file?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is there a bash command for picking a random file?
This might sound like a strange question, but is there any bash command that I can use to pick a random file in a directory? For example, can I use it to play a random video file in the directory /home/andrew/Video by typing "totem /home/andrew/Video/[whatever the command may be]"?
Basically, what I'm wanting to do, and this may sound frivolous, is to use this with kalarm. I'm currently using kalarm with the command action, and I'm using the command "totem /home/andrew/Videos/**" and I have a different file for each day of the week so I wake up to a different video each morning. It would be great if I could just find a command that would select a random file in that directory, that would mean I'd have 1 alarm entry instead of 5, and it wouldn't repeat the same one every Monday, Tuesday, etc.
You can design your own, if would be something like this:
Code:
#!/bin/bash
# Reads a given directory and picks a random file.
# The directory you want to use. You could use "$1" instead if you
# wanted to parametrize it.
DIR="$HOME/tmp"
# DIR="$1"
# Internal Field Separator set to newline, so file names with
# spaces do not break our script.
IFS='
'
if [[ -d "${DIR}" ]]
then
# Runs ls on the given dir, and dumps the output into a matrix,
# it uses the new lines character as a field delimiter, as explained above.
file_matrix=($(ls "${DIR}"))
num_files=${#file_matrix[*]}
# This is the command you want to run on a random file.
# Change "ls -l" by anything you want, it's just an example.
ls -l "${DIR}/${file_matrix[$((RANDOM%num_files))]}"
fi
exit 0
As-is, the script will register the given directory, store all the files on that dir into an array, and then show the output of ls -l for a random file on that array. You can repeat it many times to see how it shows a different file each time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.