Suppose there is a directory holding 300 data files. I want to randomly select 200 of those files and move them into another directory. Is there a way to do that under Unix/Linux?
|
If your system has
If you don't have
|
|||||||||
|
|
||||
|
|
|
Put all filenames into an array named "files" in bash:
size of array:
define 2/3 of them as sample size:
This will select duplicates, and is The simplest way to avoid duplicates is, to iterate over all files, and pick each one with 2/3 chance, but this will not necessarily lead to 200 files. This will remove a file if it was chosen from the list and fulfill your requirements:
|
|||||||||||
|
|
If this needs to be statistically random, you shouldn't use
Thus, when selecting the first item, there's a 110/32768~=0.33569% chance for each of the 68 first elements, and 109/32768~=0.33264% chance for each of the other 232 elements to be selected. Picking is repeated several times with different chances, but biased towards the first elements whenever This should be unbiased, and works with any filename:
|
||||
|
|

list.files()... – sr_ May 10 '12 at 14:29shufandhead(or just useshuf -n, should've read the man page...) – Ulrich Schwarz May 10 '12 at 14:41