Best way to choose a random file from a directory in a shell script

后端 未结 11 1531
长情又很酷
长情又很酷 2020-12-13 02:00

What is the best way to choose a random file from a directory in a shell script?

Here is my solution in Bash but I would be very interested for a more portable (non-

11条回答
  •  無奈伤痛
    2020-12-13 02:25

    Put each line of output from the command 'ls' into an associative array named line and then choose one of those like so...

    ls | awk '{ line[NR]=$0 } END { print line[(int(rand()*NR+1))]}'
    

提交回复
热议问题