如何从bash中的目录中选择随机文件? [英] How can I select random files from a directory in bash?

查看:24
本文介绍了如何从bash中的目录中选择随机文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大约 2000 个文件的目录.如何通过使用 bash 脚本或管道命令列表来选择 N 个文件的随机样本?

I have a directory with about 2000 files. How can I select a random sample of N files through using either a bash script or a list of piped commands?

推荐答案

这是一个使用 GNU sort 的随机选项的脚本:

Here's a script that uses GNU sort's random option:

ls |sort -R |tail -$N |while read file; do
    # Something involving $file, or you can leave
    # off the while to just get the filenames
done

这篇关于如何从bash中的目录中选择随机文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆