使用shell脚本生成随机文件
如何在shell脚本中生成一个充满随机数字或字符的随机文件?我还想指定文件的大小。
How can i generate a random file filled with random number or character in shell script? I also want to specify size of the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
dd
命令从/dev/random读取数据。这将读取 5000 个 1MB 的随机数据块,即总共 5GB 的随机数据!
尝试使用块大小参数以获得最佳性能。
Use
dd
command to read data from /dev/random.That would read 5000 1MB blocks of random data, that is a whole 5 gigabytes of random data!
Experiment with blocksize argument to get the optimal performance.
将 10 更改为任意值。阅读“man random”了解 /dev/random 和 /dev/urandom 之间的差异。
或者,仅适用于 Base64 字符
Base64 可能包含一些您不感兴趣的字符,但没有时间想出更好的单行字符转换器...
(而且我们从 /dev/random 中获取了太多字节。抱歉,熵池!)
change 10 to whatever. Read "man random" for differences between /dev/random and /dev/urandom.
Or, for only base64 characters
The base64 might include some characters you're not interested in, but didn't have time to come up with a better single-liner character converter...
(also we're taking too many bytes from /dev/random. sorry, entropy pool!)
一个好的开始是:
http://linuxgazette.net/153/pfeiffer.html
A good start would be:
http://linuxgazette.net/153/pfeiffer.html
创建 100 个随机命名的文件,每个文件大小 50MB:
Create 100 randomly named files of 50MB in size each:
RANDOM 变量每次都会给你一个不同的数字:
The RANDOM variable will give you a different number each time:
另存为“script.sh”,以./script.sh SIZE 运行。 printf 代码取自 http://mywiki.wooledge.org/BashFAQ/071。当然,你可以用蛮力初始化 mychars 数组, mychars=("0" "1" ... "A" ... "Z" "a" ... "z"),但这不会有趣吗?
Save as "script.sh", run as ./script.sh SIZE. The printf code was lifted from http://mywiki.wooledge.org/BashFAQ/071. Of course, you could initialize the mychars array with brute force, mychars=("0" "1" ... "A" ... "Z" "a" ... "z"), but that wouldn't be any fun, would it?