LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 10-23-2008, 08:11 PM   #1
adanedhel728
Member
 
Registered: Sep 2007
Distribution: Ubuntu Lucid
Posts: 122

Rep: Reputation: 21
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.

Thanks!
--Andrew
 
Old 10-23-2008, 09:22 PM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 386Reputation: 386Reputation: 386Reputation: 386
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.

I hope it's of any use.
 
Old 10-23-2008, 09:48 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 335

Rep: Reputation: 141Reputation: 141
If you have the 'shuf' command, which is part of the GNU Core Utilities,
http://www.gnu.org/software/coreutil...nvocation.html
Code:
cd /home/andrew/Videos; totem "$(ls | shuf -n1)"
 
Old 10-23-2008, 10:13 PM   #4
adanedhel728
Member
 
Registered: Sep 2007
Distribution: Ubuntu Lucid
Posts: 122

Original Poster
Rep: Reputation: 21
Awesome, both of those suggestions work perfectly, and I got a good little introduction to scripting that might prove useful in the future

Thanks to both of you!
--Andrew
 
Old 10-24-2008, 11:15 AM   #5
grumpf
Member
 
Registered: Dec 2005
Distribution: just replace windows with linux
Posts: 38

Rep: Reputation: 15
what is about "mktemp" ?
 
Old 10-24-2008, 12:03 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 386Reputation: 386Reputation: 386Reputation: 386
Quote:
Originally Posted by grumpf View Post
what is about "mktemp" ?
On a first thought, mktemp is not related to the purpose of this thread. What do you mean?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash - choose a random line from a file ericcarlson Linux - Software 27 11-12-2013 01:25 AM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
BASH command for open file phantom_cyph Linux - General 16 03-26-2007 10:26 PM
Random file lines directed to a new file. In script an error. In command line no err leventis Programming 1 09-28-2006 07:16 AM
random picking scripts inside a script mrgreaper Linux - Newbie 5 06-13-2006 08:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration