由于ls按字母顺序返回文件,有没有办法以随机顺序返回相同的文件?我试图循环遍历目录中的所有文件,但希望它在单独的运行中有所不同。
for i in *.py # Would like order to be random
do
...
done
Run Code Online (Sandbox Code Playgroud)
这是bash 中随机打乱文件的副本以及How can I shuffle the rows of a text file on the Unix command line or in a shell script?
然而,这应该可以完成这项工作:
for i in `ls *.py | shuf`
do
echo $i
done
Run Code Online (Sandbox Code Playgroud)