Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda