由于ls
以字母顺序返回文件,是否有办法以随机顺序返回相同的文件?我试图遍历目录中的所有文件,但希望它在单独的运行中有所不同。
for i in *.py # Would like order to be random
do
...
done
答案 0 :(得分:2)
这是randomly shuffling files in bash和How 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