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

标签 linux ubuntu command-line

我读过这个问题: 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/

相关文章:

arrays - 将值插入数组并显示,nasm

linux - Ubuntu 16.04 linux-crashdump 失败

ubuntu - gasLimit 随时间减少

windows - Visual Studio 2017 : open AND build solution from a command line call

c - 全局按键监听器

python - 在 Linux 中,如何从命令行获取我正在使用的 Python 二进制文件的绝对路径?

selenium - 如何告诉 Webdriver 使用特定版本的 ChromeDriver?

angular - compodoc:找不到命令

c++ - 如何在没有硬件抽象层的情况下直接访问x64硬件?

c - 如何接收来自具有相同IP地址的节点的广播?