Linux 随机数据生成

目录

  • [一. `/dev/urandom`](#一. /dev/urandom)
    • [1.1 dd 命令](#1.1 dd 命令)
    • [1.2 head命令](#1.2 head命令)
    • [1.3 随机字母](#1.3 随机字母)
  • [二. openssl 命令](#二. openssl 命令)
  • [三. yes命令](#三. yes命令)

一. /dev/urandom

/dev/urandom 是 Linux 和 Unix 系统中的一个特殊文件,它是一个伪随机数生成器,用于提供高吞吐量的随机数据。

1.1 dd 命令

  • bs=1M count=10:每次写入1M的文件,共写入10次
  • status=progress:用于在 dd 命令执行过程中显示进度信息
bash 复制代码
dd if=/dev/urandom bs=1M count=10 of=test_content.txt status=progress

1.2 head命令

bash 复制代码
head -c 10M /dev/urandom > test_content.txt
# 随机数据被转换被 base64 编码处理
head -c 10M /dev/urandom | base64 > test_content.txt

1.3 随机字母

  • 随机生成10个随机字母
bash 复制代码
cat /dev/urandom | tr -dc 'a-zA-Z' | head -c 10

⏹随机生成1M的随机字母+数字字符串

  • -d:删除(delete)
  • -c:取反(complement)
  • 删除除了a-zA-Z0-9之外的所有字符
bash 复制代码
tr -dc 'a-zA-Z0-9' </dev/urandom | head -c 1M

二. openssl 命令

  • rand:生成随机字节数据
  • -base64:随机数据编码为 Base64 格式
  • -hex:随机数据编码为 十六进制格式
  • $((1024*1024*10)):10M的数据
bash 复制代码
openssl rand -base64 $((1024*1024*10)) -out test_content.txt
openssl rand -hex $((1024*1024*10)) -out test_content.txt

三. yes命令

⏹yes 命令是 Linux 中一个有趣且实用的工具,主要用于不断输出某个字符串,常见用途包括

  • 自动确认提示
  • 压力测试等
bash 复制代码
# 因为不能让yes命令一直执行下去,所以通过head命令限制输出文件的大小
yes "hello world" | head -c 10M > repeated.txt
相关推荐
阿里云大数据AI技术2 小时前
阿里云 EMR AI 助手正式发布:从问答工具到全栈智能运维助手
运维·人工智能
orion5719 小时前
Missing Semester Class1:course overview and introduction of shell
linux
SkyWalking中文站1 天前
认识 Horizon UI · 6/17:Trace 探索器
运维·监控·自动化运维
用户120487221611 天前
Linux驱动编译与加载
linux·嵌入式
火车叼位1 天前
写给初级开发者:SSL、SSH、HTTPS 与证书体系全解析
运维
用户805533698031 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698031 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
小猿姐2 天前
唯品会大规模数据库云原生实践:基于 KubeBlocks 管理数千实例的统一运维之路
运维·elasticsearch·云原生
七歌杜金房2 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
SkyWalking中文站2 天前
认识 Horizon UI · 5/17:3D 基础设施地图
运维·监控·自动化运维