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

时间:2022-01-24 13:16:54

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上创建特定大小的文件的命令是什么?

12 个解决方案

#1


156  

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

Where file_size is the size of your test file in bytes

文件大小是测试文件的字节大小

#2


133  

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

拜托,现代更容易,也更快。在Linux上,(选择一个)

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

It needs to be stated that truncate on a file system supporting sparse files will create a sparse file and fallocate will not. A sparse file is one where the allocation units that make up the file are not actually allocated until used. The meta-data for the file will however 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 fallocate nor truncate will not set the contents of the file to a specified value like dd, instead the contents of a file allocated with fallocate or truncate may be any trash value that existed in the allocated units during creation and this behavior may or may not be desired. The dd is 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.

需要说明的是,在支持稀疏文件的文件系统上截断将创建稀疏文件,而错误定位将不会。稀疏文件是指组成文件的分配单元直到使用后才实际分配的文件。不过,该文件的元数据将占用相当大的空间,但在接近文件实际大小的地方可能没有。您应该查阅有关稀疏文件的参考资料以获得更多信息,因为这种类型的文件有优点和缺点。非稀疏文件有它的块(分配单元)提前分配,这意味着空间在文件系统看到的范围内被保留。也fallocate也截断不会设置文件的内容到指定的值像弟弟一样,而不是一个文件的内容分配与fallocate或截断可能存在任何垃圾值在创建和分配单位这种行为可能是也可能不是理想。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.

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

#3


37  

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

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

dd if=/dev/zero of=the_file bs=1 count=0 seek=12345

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上创建一个带有“漏洞”的文件——数据实际上不会写到磁盘上,也不会占用任何空间,除非将非零的内容写到其中。

#4


20  

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/ 0。文件的总大小将是$块大小* $数字块。新创建的文件将是$OUTPUT-FILE。

#5


19  

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

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

mkfile 10g big_file

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

这将生成一个名为“big_file”的10gb文件。发现这个方法。

#6


17  

You can do it programmatically:

您可以通过编程方式完成:

#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 approach is especially useful to subsequently mmap the file into memory.

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

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

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

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

Be careful:

小心:

# du /tmp/foo.txt

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

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

see also: man 2 open and man 2 truncate

参见:man 2打开,man 2截断

#7


10  

you could do:

你能做的:

[dsm@localhost:~]$ perl -e 'print "\0" x 100' > filename.ext

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

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

#8


10  

Some of these answers have you using /dev/zero for 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 really well. Using this command to generate the file

其中一些答案要求您使用/dev/ 0作为数据源。如果您的测试网络上传速度,如果您的应用程序正在执行任何压缩,那么这可能不是最好的方法,因为满是0的文件压缩得非常好。使用此命令生成文件

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

I could compress upload_test down 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/urandom instead of /dev/zero. I couldn't compress the output of /dev/urandom very much at all.

我的建议是使用/dev/urandom而不是/dev/zero.我不能压缩/dev/urandom的输出。

#9


9  

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

#10


3  

As shell command:

shell命令:

< /dev/zero head -c 1048576 >  output

#11


3  

There are lots of answers, but none explained nicely what else can be done. Looking into man pages for dd, it is possible to better specify the size of a file.

有很多答案,但没有一个能很好地解释其他可以做的事情。在手册页中查找dd,可以更好地指定文件的大小。

This is going to create /tmp/zero_big_data_file.bin filled with zeros, that has size of 20 megabytes :

这将创建/tmp/zero_big_data_file。bin填充0,大小为20兆字节:

    dd if=/dev/zero of=/tmp/zero_big_data_file.bin  bs=1M count=20

This is going to create /tmp/zero_1000bytes_data_file.bin filled with zeros, that has size of 1000 bytes :

这将创建/tmp/zero_1000bytes_data_file。bin填充了0,它的大小为1000字节:

    dd if=/dev/zero of=/tmp/zero_1000bytes_data_file.bin  bs=1kB count=1

or

    dd if=/dev/zero of=/tmp/zero_1000bytes_data_file.bin  bs=1000 count=1

  • In all examples, bs is block size, and count is number of blocks
  • 在所有示例中,bs是块大小,count是块的数量
  • BLOCKS and BYTES may be followed by the following multiplicative suffixes: c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.
  • 块和字节后面可能有以下乘法后缀:c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM = mgb =1000*1000*1000, G =1024*1024*1024,等等。

#12


2  

This will generate 4 MB text file with random characters in current directory and its name "4mb.txt" You can change parameters to generate different sizes and names.

这将生成4 MB的文本文件,其中包含当前目录中的随机字符及其名称“4mb”。您可以更改参数以生成不同的大小和名称。

base64 /dev/urandom | head -c 4000000 > 4mb.txt

#1


156  

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

Where file_size is the size of your test file in bytes

文件大小是测试文件的字节大小

#2


133  

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

拜托,现代更容易,也更快。在Linux上,(选择一个)

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

It needs to be stated that truncate on a file system supporting sparse files will create a sparse file and fallocate will not. A sparse file is one where the allocation units that make up the file are not actually allocated until used. The meta-data for the file will however 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 fallocate nor truncate will not set the contents of the file to a specified value like dd, instead the contents of a file allocated with fallocate or truncate may be any trash value that existed in the allocated units during creation and this behavior may or may not be desired. The dd is 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.

需要说明的是,在支持稀疏文件的文件系统上截断将创建稀疏文件,而错误定位将不会。稀疏文件是指组成文件的分配单元直到使用后才实际分配的文件。不过,该文件的元数据将占用相当大的空间,但在接近文件实际大小的地方可能没有。您应该查阅有关稀疏文件的参考资料以获得更多信息,因为这种类型的文件有优点和缺点。非稀疏文件有它的块(分配单元)提前分配,这意味着空间在文件系统看到的范围内被保留。也fallocate也截断不会设置文件的内容到指定的值像弟弟一样,而不是一个文件的内容分配与fallocate或截断可能存在任何垃圾值在创建和分配单位这种行为可能是也可能不是理想。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.

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

#3


37  

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

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

dd if=/dev/zero of=the_file bs=1 count=0 seek=12345

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上创建一个带有“漏洞”的文件——数据实际上不会写到磁盘上,也不会占用任何空间,除非将非零的内容写到其中。

#4


20  

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/ 0。文件的总大小将是$块大小* $数字块。新创建的文件将是$OUTPUT-FILE。

#5


19  

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

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

mkfile 10g big_file

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

这将生成一个名为“big_file”的10gb文件。发现这个方法。

#6


17  

You can do it programmatically:

您可以通过编程方式完成:

#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 approach is especially useful to subsequently mmap the file into memory.

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

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

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

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

Be careful:

小心:

# du /tmp/foo.txt

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

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

see also: man 2 open and man 2 truncate

参见:man 2打开,man 2截断

#7


10  

you could do:

你能做的:

[dsm@localhost:~]$ perl -e 'print "\0" x 100' > filename.ext

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

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

#8


10  

Some of these answers have you using /dev/zero for 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 really well. Using this command to generate the file

其中一些答案要求您使用/dev/ 0作为数据源。如果您的测试网络上传速度,如果您的应用程序正在执行任何压缩,那么这可能不是最好的方法,因为满是0的文件压缩得非常好。使用此命令生成文件

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

I could compress upload_test down 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/urandom instead of /dev/zero. I couldn't compress the output of /dev/urandom very much at all.

我的建议是使用/dev/urandom而不是/dev/zero.我不能压缩/dev/urandom的输出。

#9


9  

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

#10


3  

As shell command:

shell命令:

< /dev/zero head -c 1048576 >  output

#11


3  

There are lots of answers, but none explained nicely what else can be done. Looking into man pages for dd, it is possible to better specify the size of a file.

有很多答案,但没有一个能很好地解释其他可以做的事情。在手册页中查找dd,可以更好地指定文件的大小。

This is going to create /tmp/zero_big_data_file.bin filled with zeros, that has size of 20 megabytes :

这将创建/tmp/zero_big_data_file。bin填充0,大小为20兆字节:

    dd if=/dev/zero of=/tmp/zero_big_data_file.bin  bs=1M count=20

This is going to create /tmp/zero_1000bytes_data_file.bin filled with zeros, that has size of 1000 bytes :

这将创建/tmp/zero_1000bytes_data_file。bin填充了0,它的大小为1000字节:

    dd if=/dev/zero of=/tmp/zero_1000bytes_data_file.bin  bs=1kB count=1

or

    dd if=/dev/zero of=/tmp/zero_1000bytes_data_file.bin  bs=1000 count=1

  • In all examples, bs is block size, and count is number of blocks
  • 在所有示例中,bs是块大小,count是块的数量
  • BLOCKS and BYTES may be followed by the following multiplicative suffixes: c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.
  • 块和字节后面可能有以下乘法后缀:c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM = mgb =1000*1000*1000, G =1024*1024*1024,等等。

#12


2  

This will generate 4 MB text file with random characters in current directory and its name "4mb.txt" You can change parameters to generate different sizes and names.

这将生成4 MB的文本文件,其中包含当前目录中的随机字符及其名称“4mb”。您可以更改参数以生成不同的大小和名称。

base64 /dev/urandom | head -c 4000000 > 4mb.txt