Pick random file from ls command.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pick random file from ls command.
# 1  
Old 02-04-2010
Pick random file from ls command.

Lets say I want to pick a random file when I do an "ls" command. I don't have set number of files in each directory.

Code:
ls | head -1

This gives me the first one in each directory, is there a way to do the same but pick a random one.
# 2  
Old 02-04-2010
A bit odd, but...
Code:
ls | sed $((RANDOM%$(ls | wc -w)+1))!d\;q

or
Code:
ls | head -$((RANDOM%$(ls | wc -w)+1)) | tail -1

# 3  
Old 02-04-2010
First one didn't work
ls | sed $((RANDOM%$(ls | wc -w)+1))!d
-bash: !d: event not found

The second one works perfectly. Can you help me understand how it works? thanks.
# 4  
Old 02-04-2010
Hey, you're right!

Works in KSH (on AIX) but not in Bash.

Code:
ls | sed -n "$((RANDOM%$(ls | wc -l)+1))p"

(changed wc to use -l)

Quote:
Originally Posted by elbombillo
Can you help me understand how it works? thanks.
  • do an ls, and pipe to output to sed
  • get a random number with modulo of the number of files (i.e. pick a number between 1 and the number of files in the directory). $RANDOM is a special variable that returns a "random" number between 0 and 32K-1
  • tell sed not to print anything except that line number (i.e. that file)

Last edited by Scott; 02-04-2010 at 02:46 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pick up files to process using ls command

Hello All, I have a list of files as below --------- FORM_PTIR_9484_ANFDOP_20150716_00263059.DAT FORM_PTIR_9484_TIFDOP_20150716_00263059.DAT FORM_PTIR_9484_EXFDOP_20150716_00263059.DAT FORM_PTIR_9484_ANFDEX_20150716_00263059.DAT FORM_PTIR_9484_ANFDCA_20150716_00263059.DAT and so on....... (4 Replies)
Discussion started by: rac
4 Replies

2. Shell Programming and Scripting

[Solved] Help with random pick 1000 number from range 1 to 150000

Hi, Do anybody knows how to use awk or any command to random print out 1000 number which start from range 1 to 150000? I know that "rand" in awk can do similar random selection. But I have no idea how to write a code that can random pick 1000 number from range 1 to 150000 :confused: ... (1 Reply)
Discussion started by: perl_beginner
1 Replies

3. Shell Programming and Scripting

Code to pick all files from a directory and send it to a command

(4 Replies)
Discussion started by: rossi
4 Replies

4. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

5. UNIX for Dummies Questions & Answers

Need command to pick the latest file

Hi In my script i am trying to access mainframe server using FTP, in the server i have filee with the timestamp.I need to get the file with the latest timestamp among them . The server has the below files / ftp> cd /outbox 250 CWD command successful ftp> ls 200 PORT command successful... (4 Replies)
Discussion started by: laxmi131
4 Replies

6. Shell Programming and Scripting

Pick one file from each subdirectory

Hi, I have a problem I am trying to solve with bash. I need to search in a file system (data base) with hundreds of directories and thousands of subdirectories and millions of files. The files have a specific format with a header that gives the properties. Directories are organized so... (1 Reply)
Discussion started by: majest
1 Replies

7. Shell Programming and Scripting

How to Pick Random records from a large file

Hi, I have a huge file say with 2000000 records. The file has 42 fields. I would like to pick randomly 1000 records from this huge file. Can anyone help me how to do this? (1 Reply)
Discussion started by: ajithshankar@ho
1 Replies

8. Shell Programming and Scripting

Random command

I am trying to select one random word from a file, any ideas on how to do this as i have only manged to generete the random number? (1 Reply)
Discussion started by: melaz
1 Replies

9. Shell Programming and Scripting

How does mutt command pick senders Details

Hi All, We use mutt in one of our scripts to send files as a part of a batch job . The command works nicely but i want to know from where does mutt pick the sender details. As the sender id is different if i use mutt and Mail utility. I can not change the command to mail as it is a part of the... (1 Reply)
Discussion started by: amit1_x
1 Replies
Login or Register to Ask a Question