gpt4 book ai didi

linux - 如何在 Linux 中创建任意给定大小的文件?

转载 作者:IT王子 更新时间:2023-10-29 00:08:56 28 4
gpt4 key购买 nike

我读过这个问题: How to create a file with a given size in Linux?

但是我的问题还没有得到答案。

我想创建一个 372.07 MB 的文件,

我在 Ubuntu 10.08 中尝试了以下命令:

dd if=/dev/zero of=output.dat  bs=390143672  count=1
dd: memory exhausted

390143672=372.07*1024*1024

还有其他方法吗?

非常感谢!

编辑:如何在 Linux 命令行上使用十进制查看文件大小。我的意思是,命令行 ls -hl 只是说:“373M”,但文件实际上是“372.07M”。

最佳答案

稀疏文件

dd of=output.dat bs=1 seek=390143672 count=0

如果底层文件系统支持,这还有一个额外的好处,即创建文件稀疏。这意味着,如果某些页面 (_blocks) 被写入并且文件创建速度非常快不会浪费任何空间


非稀疏(不透明)文件:

Edit since people have, rightly pointed out that sparse files have characteristics that could be disadvantageous in some scenarios, here is the sweet point:

您可以使用 fallocate(由于 util-linux 而存在于 Debian 中):

fallocate -l 390143672 output.dat

这仍然具有不需要实际编写 block 的好处,因此它与创建稀疏文件一样快但是不稀疏。两全其美。

关于linux - 如何在 Linux 中创建任意给定大小的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7965530/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com