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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/139261/
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-03 16:32:00  来源:igfitidea点击:

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

linuxcommand-line

提问by Grundlefleck

For testing purposes I have to generate a file of a certain size (to test an upload limit).

出于测试目的,我必须生成一个特定大小的文件(以测试上传限制)。

What is a command to create a file of a certain size on Linux?

在 Linux 上创建特定大小文件的命令是什么?

采纳答案by Ilya Kochetov

dd if=/dev/zero of=upload_test bs=file_size count=1

Where file_sizeis the size of your test file in bytes

file_size测试文件的大小(以字节为单位)在哪里

回答by Grundlefleck

Use this command:

使用这个命令:

dd if=$INPUT-FILE of=$OUTPUT-FILE bs=$BLOCK-SIZE count=$NUM-BLOCKS

To create a big (empty) file, set $INPUT-FILE=/dev/zero.
Total size of the file will be $BLOCK-SIZE * $NUM-BLOCKS.
New file created will be $OUTPUT-FILE.

要创建一个大(空)文件,请设置$INPUT-FILE=/dev/zero.
文件的总大小将为$BLOCK-SIZE * $NUM-BLOCKS.
创建的新文件将是$OUTPUT-FILE.

回答by Paul Tomblin

dd if=/dev/zero of=my_file.txt count=12345

回答by dsm

you could do:

你可以这样做:

[dsm@localhost:~]$ perl -e 'print "
dd if=/dev/zero of=the_file bs=1 count=0 seek=12345
" x 100' > filename.ext

Where you replace 100 with the number of bytes you want written.

将 100 替换为要写入的字节数。

回答by Brian

Just to follow up Tom'spost, you can use dd to create sparse files as well:

只是为了跟进Tom 的帖子,您也可以使用 dd 创建稀疏文件:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

int main() {
    int fd = creat("/tmp/foo.txt", 0644);
    ftruncate(fd, SIZE_IN_BYTES);
    close(fd);
    return 0;
}

This will create a file with a "hole" in it on most unixes - the data won't actually be written to disk, or take up any space until something other than zero is written into it.

这将在大多数 Unix 上创建一个带有“洞”的文件 - 数据实际上不会写入磁盘,或占用任何空间,直到将零以外的内容写入其中。

回答by Benedikt Waldvogel

You can do it programmatically:

您可以以编程方式执行此操作:

# du -B1 --apparent-size /tmp/foo.txt

This approach is especially useful to subsequently mmapthe file into memory.

这种方法对于随后将文件映射到内存中特别有用。

use the following command to check that the file has the correct size:

使用以下命令检查文件大小是否正确:

# du /tmp/foo.txt

Be careful:

当心:

mkfile 10g big_file

will probably print 0because it is allocated as Sparse fileif supported by your filesystem.

可能会打印0,因为如果您的文件系统支持它,它会被分配为稀疏文件

see also: man 2 openand man 2 truncate

另见:man 2 openman 2 truncate

回答by steve

On OSX (and Solaris, apparently), the mkfilecommand is available as well:

在 OSX(和 Solaris,显然)上,该mkfile命令也可用:

truncate -s 10G foo
fallocate -l 5G bar

This makes a 10 GB file named "big_file". Found this approach here.

这将生成一个名为“big_file”的 10 GB 文件。在这里找到了这种方法

回答by j?rgensen

Please, modern is easier, and faster. On Linux, (pick one)

请,现代更容易,更快。在 Linux 上,(选择一项)

< /dev/zero head -c 1048576 >  output

It needsto be stated that truncateon a file system supporting sparse files will create a sparse file and fallocatewill not. A sparse file is one where the allocation units that make up the file are not actuallyallocated until used. The meta-data for the file willhowever take up some considerable space but likely no where near the actual size of the file. You should consult resources about sparse files for more information as there are advantages and disadvantages to this type of file. A non-sparse file has its blocks (allocation units) allocated ahead of time which means the space is reserved as far as the file system sees it. Also fallocatenor truncatewill not set the contents of the file to a specified value likedd, instead the contents of a file allocated with fallocateor truncatemay be any trash value that existed in the allocated units during creation and this behavior may or may not be desired. The ddis the slowest because it actually writes the value or chunk of data to the entire file stream as specified with it's command line options.

需要被指出truncate的文件系统上支持稀疏文件将创建一个稀疏文件,fallocate不会。稀疏文件是这样一种文件,其中构成文件的分配单元在使用之前并未实际分配。然而,文件的元数据占用一些相当大的空间,但可能与文件的实际大小相差甚远。您应该查阅有关稀疏文件的资源以获取更多信息,因为此类文件既有优点也有缺点。非稀疏文件提前分配了它的块(分配单元),这意味着空间被保留到文件系统看到的范围内。此外fallocate,也truncate不会将文件的内容到指定值dd,取而代之的是,文件的内容分配有fallocatetruncate可能是创建期间存在于分配单元中的任何垃圾值,这种行为可能会或可能不会被需要。这dd是最慢的,因为它实际上按照命令行选项的指定将值或数据块写入整个文件流。

This behavior could potentially be different - depending on file system used and conformance of that file system to any standard or specification. Therefore it is advised that proper research is done to ensure that the appropriate method is used.

这种行为可能会有所不同 - 取决于所使用的文件系统以及该文件系统是否符合任何标准或规范。因此,建议进行适当的研究以确保使用适当的方法。

回答by user1772090

As shell command:

作为 shell 命令:

 dd if=/dev/zero of=upload_test bs=10000 count=1

回答by devin

Some of these answers have you using /dev/zerofor the source of your data. If your testing network upload speeds, this may not be the best idea if your application is doing any compression, a file full of zeros compresses reallywell. Using this command to generate the file

其中一些答案让您/dev/zero用作数据源。如果您测试网络上传速度,如果您的应用程序正在执行任何压缩,这可能不是最好的主意,一个充满零的文件压缩得非常好。使用此命令生成文件

##代码##

I could compress upload_testdown to about 200 bytes. So you could put yourself in a situation where you think your uploading a 10KB file but it would actually be much less.

我可以压缩upload_test到大约 200 个字节。因此,您可能会认为自己上传了一个 10KB 的文件,但实际上它会少得多。

What I suggest is using /dev/urandominstead of /dev/zero. I couldn't compress the output of /dev/urandomvery much at all.

我的建议是使用/dev/urandom而不是/dev/zero. 我根本无法压缩输出/dev/urandom