如何在Linux中创建任何给定大小的文件? [英] How to create a file with ANY given size in Linux?
问题描述
我读过这个问题:
如何在Linux中创建一个给定大小的文件?
但我没有回答我的问题。
我要创建一个372.07 MB的文件,
我在Ubuntu 10.08中试过下面的命令:
dd if = / dev / zero of = output.dat bs = 390143672 count = 1
dd:内存耗尽
390143672 = 372.07 * 1024 * 1024
还有其他方法吗? / p>
非常感谢!
编辑:
如何在Linux命令行上查看文件大小十进制。我的意思是,命令行ls -hl
只是说:'373M',但文件实际上是372.07M。解决方案
稀疏文件
dd of = output.dat bs = 390143672 count = 0
这具有创建文件 sparse (如果底层文件系统支持)。这意味着,如果某些网页(_blocks)被写入并且文件创建非常快,则不会浪费空间。
非稀疏(不透明)档案:
编辑,因为人们已经正确地指出,稀疏文件具有在某些情况下可能不利的特征,这里是最大的优点:
您可以使用
fallocate
(由于util-linux
)代替:fallocate -l 390143672 output.dat
这仍然具有不需要实际写入这些块的好处,快速作为创建稀疏文件 ,它是不稀疏。 Best of Both Worlds。
I have read this question: How to create a file with a given size in Linux?
But I havent got answer to my question.
I want to create a file of 372.07 MB,
I tried the following commands in Ubuntu 10.08:
dd if=/dev/zero of=output.dat bs=390143672 count=1 dd: memory exhausted
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".解决方案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.
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 toutil-linux
) instead: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.
这篇关于如何在Linux中创建任何给定大小的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!