Unix:ls是否可以选择所有文件,但顺序是随机的?

时间:2018-08-17 06:06:06

标签: unix ls

由于ls以字母顺序返回文件,是否有办法以随机顺序返回相同的文件?我试图遍历目录中的所有文件,但希望它在单独的运行中有所不同。

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

1 个答案:

答案 0 :(得分:2)

这是randomly shuffling files in bashHow can I shuffle the lines of a text file on the Unix command line or in a shell script?

的副本

但是,这应该可以完成工作:

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