Getting a random file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting a random file
# 1  
Old 12-31-2006
Getting a random file

Hello, I am very new to shell scripting. This problem seems quite easy so it should be quite easy (I hope ^^)
I want to get a random file from a directory. this file will be in one subdirectory, and it will contain spaces.

code I have got so far:

N=find ./*/*.jpg | wc -l
((N=RANDOM%N))

That gives me a number. Now I want to select the Nth file in this file list generated by find ./*/*.jpg

I wanted to use echo ./*/*.jpg except that gave me spaces as a delimiter, which is kind of stupid ^^, since file names contain spaces
# 2  
Old 12-31-2006
***code removed***

Please see the rules, no classroom/homework posts allowed.
# 3  
Old 12-31-2006
I have reopend this thread becasue the member gave a satisfactory justification for the thread by PM.

Sorry David, seems I was a bit hasty. For future reference if you explain what you are trying to achieve it might avoid this, in particular since you are posting from an educational domain.

ksh code:
Code:
set +A files ./*/*.jpg    # create an array of the files.
N=${#files[@]}           # Number of members in the array
((N=RANDOM%N))
randomfile=${files[$N]}

bash:
Code:
files=(./*/*.jpg)        # create an array of the files.
N=${#files[@]}          # Number of members in the array
((N=RANDOM%N))
randomfile=${files[$N]}

# 4  
Old 12-31-2006
ksh arrays are small. Only 1024 is guaranteed. bash does not seem to have an arbritrary limit.
# 5  
Old 12-31-2006
My version of ksh (1993-12-28 q) doesn't seem to have a limit either. I did a test to assign values to a array in a loop. I gave up at 700,000 elements.

ksh seems to be twice as fast than bash to assign array values. So, I pushed my little benchmark a little bit further and I assigned 10,000 values to an array and randomly accessed that array 1000 times.

Code:
bash
  real    0m0.623s
  user    0m0.500s
  sys     0m0.020s

ksh
  real    0m0.157s
  user    0m0.160s
  sys     0m0.000s

Happy new year to all. Smilie
# 6  
Old 12-31-2006
I looked it up in Korn's book: "The subscript for an indexed array can be any arithmetic expression that evaluates between 0 and some implementation-defined value that is at least 4095.

And the O'reilly bash book says: "up to 599147937791" (Of course, you will probably run out of virtual memory before that.)

Also ksh is faster than bash for anything that loops because ksh compiles the loop. bash re-interprets the loop each iteration.
# 7  
Old 01-01-2007
THanks guys! I hope my wallpaper will now change every 1 hour ^_^
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recurse directories and return random file

I have a nice program to change the background but I want it to operate on subdirectories as well. # Script to randomly set Background from files in a directory while true;do # Directory Containing Pictures DIR="/home/pc/Pictures" # Internal Field Separator set to newline, so file names... (4 Replies)
Discussion started by: triplemaya
4 Replies

2. 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

3. Shell Programming and Scripting

choosing random columns from a file

Hello, I want to choose random columns from big file. for example: My file contain around 21000 columns and I want to randomly extract 4000 columns from this file. Anybody has a solution (may be one liner or a function in perl or awk) for this? Thanks, R (2 Replies)
Discussion started by: ryan9011
2 Replies

4. Shell Programming and Scripting

Random File Selection and Moving

OK, I am stumpped. I have this shell Script that I want to randomly select a file with the extention of .sct. Then using a portion of its file name select the six related .mot files. Then move them all to another folder. I also need a user input form for the number of .SCT files to randomly select... (6 Replies)
Discussion started by: stak1993
6 Replies

5. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

6. Shell Programming and Scripting

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. 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. (3 Replies)
Discussion started by: elbombillo
3 Replies

7. Shell Programming and Scripting

random select text file ranamed

i want to need script.. source.txt /home/user1/public_html/test3 /home/user90/public_html/test9 . . . /home/user650/public_html/test000 read source.txt and cd /home/user**/public_html/*** and there is 1.txt, 2txt ~~25.txt and select 6 text files randomly among the... (4 Replies)
Discussion started by: topic32428285
4 Replies

8. Shell Programming and Scripting

how to change the current file processing to some other random file in awk ?

Hello, say suppose i am processing an file emp.dat the field of which are deptno empno empname etc now say suppose i want to change the file to emp.lst then how can i do it? Here i what i attempted but in vain BEGIN{ system("sort emp.dat > emp.lst") FILENAME="emp.lst" } { print... (2 Replies)
Discussion started by: salman4u
2 Replies

9. Linux

random number as file owner

Dear all, I get the situation from my office but i cannot think what is happening. please give me some advise. 1. some file owner and group is changed to a very long number 2. i login as root. when i touch a file or operate on the file like chown, cp, the response is "Read-only file system".... (1 Reply)
Discussion started by: rickhlwong
1 Replies

10. Programming

[C++] File I/O (Reading from a Random-Access File)

INFO: The program should enter a circle radius and Id for that circle to a file, then it should search for that id and print the radius for that circle. PROBLEM: This program compiles but it's not searching properly. Circle.h #ifndef CIRCLE_H #define CIRCLE_H #include <iostream>... (0 Replies)
Discussion started by: VersEtreOuNe
0 Replies
Login or Register to Ask a Question