简体   繁体   English

如何在Linux中获得随机文件? 包括子目录

[英]How can I get a random file in linux? including subdirectories

Lets say I am in /home/myuser 可以说我在/ home / myuser

there are 90,000 files there inside 3000 directories. 3000个目录中有90,000个文件。

How can I write a bash function or with linux commands to get one random file? 如何编写bash函数或使用linux命令获取一个随机文件?

It could be C as well I suppose 我想也可能是C

You can list all your files and then pick a random line between them: 您可以列出所有文件,然后在它们之间随机选择一条线:

find /home/myuser | sort -R | head -n1

However this is not very efficient, and could take a while, but is easy to understand. 但是,这不是很有效,可能需要一段时间,但很容易理解。 You can work from here. 您可以在这里工作。

您可以将shuf用于此任务,例如set globstar选项并尝试

shuf -e path/**/*.txt | head -n1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM