Skip to content
Advertisement

How to find random files in Linux shell

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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement