2

How to pick 100 files randomly from a directory by Linux shell. I read other topic, 'shuf' command can do this: find . -type f | shuf -n100, but our environments do not have 'shuf' cmd. Is there other method to do this? use bash, awk, sed or sth else?

Allen
  • 153
  • 1
  • 1
  • 8
  • The challenge in POSIX shell is you have no array to provide a simple correlation between existing filename and random index. While there are several solutions with `awk` and via a brute force script, your must have a method that will handle (skip) duplicate random numbers generated within the range of the number of files. (which in itself is an additional challenge without a convenient array). That said, does your `sort` command provide the `-g` and `-u` options? – David C. Rankin Feb 14 '16 at 07:53

3 Answers3

2

You can get a directory listing, then randomize it, then pick the top N lines.

ls | sort -R | head -n 100

Replace ls with an appropriate find command if you want a recursive listing or need finer control of the files to be included.

5gon12eder
  • 24,280
  • 5
  • 45
  • 92
1

This should work on your CentOS5:

shuf() { awk 'BEGIN{srand()}{print rand()"\t"$0}' "$@" | sort | cut -f2- ;}

This comes from a comment by Meow on https://stackoverflow.com/a/2153889/5844347

Use like so: find . -type f | shuf | head -100

Community
  • 1
  • 1
dan4thewin
  • 1,134
  • 7
  • 10
0
# To get a integer number between 1 to 100 :
N=`echo|awk 'srand() {print 99*rand() + 1 }' | sed -e "s/\..*$//g"`
echo $N

# To get the Nth file :
find . -type f  | head -${N} | tail -1

# To get 100 files randomly :
for i in $(seq 1 100 )
N=`echo|awk 'srand() {print 99*rand() + 1 }' | sed -e "s/\..*$//g"` 
find . -type f  | head -${N} | tail -1
done
Ali ISSA
  • 398
  • 2
  • 10