Linux 命令行 创建指定大小的文件
经常有看到VPS、服务器等主机商有提供个例如100MB-fremont.bin、100mb.test等的文件,供用户下载测试网络使用。
本文介绍下如何在Linux,命令行下创建指定大小的文件(这里以创建13MB大小的文件onebox.test为例)。
truncate
truncate -s 13M onebox.test

fallocate
fallocate -l 13000000 onebox.test

dd
dd if=/dev/urandom of=onebox.test bs=13MB count=1
OR
head -c 13MB /dev/urandom > onebox.test

dd if=/dev/zero of=onebox.test bs=13MB count=1
OR
head -c 13MB /dev/zero > onebox.test

额外
13MB=13*1000*1000字节;如果想要13*1024*1024,则替换MB为M,示例:
dd if=/dev/urandom of=onebox.test bs=13M count=1
head -c 13M /dev/zero > onebox.test

命令fallocate只支持字节(bytes)单位,如果想创建13*1024*1024大小的文件,则13*1024*1024=13631488:
fallocate -l 13631488 onebox.test

本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。