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

时间:2022-01-24 13:17:06

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 -hl just says: '373M' but the file is actually "372.07M".

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

4 个解决方案

#1


16  

Sparse file

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

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

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


Non-sparse (opaque) file:

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

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

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

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

fallocate -l 390143672 output.dat

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

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

#2


7  

Change your parameters:

更改您的参数:

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

otherwise dd tries 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 seek option 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

#3


1  

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

#4


0  

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

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

#1


16  

Sparse file

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

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

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


Non-sparse (opaque) file:

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

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

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

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

fallocate -l 390143672 output.dat

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

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

#2


7  

Change your parameters:

更改您的参数:

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

otherwise dd tries 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 seek option 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

#3


1  

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

#4


0  

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

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