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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7965530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:56:16  来源:igfitidea点击:

How to create a file with ANY given size in Linux?

linuxubuntucommand-line

提问by DocWiki

I have read this question: How to create a file with a given size in Linux?

我读过这个问题: 如何在 Linux 中创建一个给定大小的文件?

But I havent got answer to my question.

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

I want to create a file of 372.07 MB,

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

I tried the following commands in Ubuntu 10.08:

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

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

390143672=372.07*1024*1024

390143672=372.07*1024*1024

Is there any other methods?

还有其他方法吗?

Thanks a lot!

非常感谢!

Edit: How to view a file's size on Linux command line with decimal. I mean, the command line ls -hljust says: '373M' but the file is actually "372.07M".

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

采纳答案by sehe

Sparse file

稀疏文件

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

This has the added benefit of creating the file sparseif the underlying filesystem supports that. This means, no space is wastedif some of the pages (_blocks) ever get written to and the file creation is extremely quick.

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



Non-sparse (opaque) file:

非稀疏(不透明)文件:

Editsince people have, rightlypointed out that sparse files have characteristics that could be disadvantageous in somescenarios, here is the sweet point:

编辑,因为人们正确地指出稀疏文件具有在某些情况下可能不利的特征,这里是甜蜜点:

You could use fallocate(in Debian present due to util-linux) instead:

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

fallocate -l 390143672 output.dat

This still has the benefit of not needing to actually writethe blocks, so it is pretty much as quickas creating the sparse file, butit is not sparse. Best Of Both Worlds.

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

回答by Mat

Change your parameters:

更改参数:

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

otherwise ddtries to create a 370MB buffer in memory.

否则dd尝试在内存中创建一个 370MB 的缓冲区。

If you want to do it more efficiently, write the 372MB part first with large-ish blocks (say 1M), then write the tail part with 1 byte blocks by using the seekoption to go to the end of the file first.

如果您想更有效地执行此操作,请先使用大块(比如 1M)写入 372MB 的部分,然后使用seek首先转到文件末尾的选项将尾部部分写入 1 个字节的块。

Ex:

前任:

dd if=/dev/zero of=./output.dat bs=1M count=1
dd if=/dev/zero of=./output.dat seek=1M bs=1 count=42

回答by thejh

Swap count and bs. bs bytes will be in memory, so it can't be that big.

交换计数和 bs。bs 字节将在内存中,所以它不能那么大。

回答by Hardy Feng

truncate - shrink or extend the size of a file to the specified size

truncate - 将文件的大小缩小或扩展到指定的大小

The following example truncates putty.log from 298 bytes to 235 bytes.

以下示例将 putty.log 从 298 字节截断为 235 字节。

root@ubuntu:~# ls -l putty.log 
-rw-r--r-- 1 root root 298 2013-10-11 03:01 putty.log
root@ubuntu:~# truncate putty.log -s 235
root@ubuntu:~# ls -l putty.log 
-rw-r--r-- 1 root root 235 2013-10-14 19:07 putty.log