Search code examples
unixls

Unix: ls to select all files, but in a random order?


Since ls returns the files in alphabetical order, is there a way to return the same files but in a random order? I am trying to loop through all the files in a directory, but would like it to differ in separate runs.

for i in *.py # Would like order to be random
do
    ...
done

Solution

  • This is a duplicate of randomly shuffling files in bash and How can I shuffle the lines of a text file on the Unix command line or in a shell script?

    However, this should do the job:

    for i in `ls *.py | shuf`
    do
        echo $i
    done