【问题标题】:Unix: ls to select all files, but in a random order?Unix: ls 选择所有文件,但以随机顺序?
【发布时间】:2018-08-17 06:06:06
【问题描述】:

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

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

【问题讨论】:

    标签: unix ls


    【解决方案1】:

    这是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
    

    【讨论】:

    • 请注意,如果文件夹中没有python文件,则会出错。
    • 这个类似,但即使有\n ... find . -name \*.py -print0 | shuf -z | xargs -0 -L 1 sh -c 'for i ; do printf "%s\n" "${i}" ; done' notusebutmandatory的文件名也会成功
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多