Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!