如何在Linux (Red Hat Linux)系统中快速创建大文件?

dd可以完成这项工作,但是从/dev/zero读取并写入驱动器可能会花费很长时间,因为您需要一个几百gb大小的文件进行测试……如果你需要重复这样做,时间就会增加。

我不关心文件的内容,我只想快速创建它。如何做到这一点呢?

使用稀疏文件不能解决这个问题。我需要为文件分配磁盘空间。


当前回答

其中seek是所需文件大小(以字节为单位)的示例

#kilobytes
dd if=/dev/zero of=filename bs=1 count=0 seek=200K

#megabytes
dd if=/dev/zero of=filename bs=1 count=0 seek=200M

#gigabytes
dd if=/dev/zero of=filename bs=1 count=0 seek=200G

#terabytes
dd if=/dev/zero of=filename bs=1 count=0 seek=200T

从dd手册页:

block和BYTES后面可以跟着下面的乘法后缀:c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000, M=1024*1024, GB =1000*1000*1000, G=1024*1024*1024,对于T, P, E, Z, Y,依次类推。

其他回答

来自其他答案的Dd是一个很好的解决方案,但它的速度较慢。在Linux(和其他POSIX系统)中,我们有fallocate,它使用所需的空间,而不必实际写入它,与大多数现代基于磁盘的文件系统一起工作,非常快:

例如:

fallocate -l 10G gentoo_root.img
truncate -s 10M output.file

将立即创建一个10m的文件(M代表10241024字节,MB代表10001000 -与K, KB, G, GB…相同)

编辑:正如许多人指出的那样,这将不会在您的设备上物理分配文件。这样你就可以创建一个任意的大文件,而不管设备上的可用空间有多大,因为它创建了一个“稀疏”文件。

例如,注意到这个命令没有占用硬盘空间:

### BEFORE
$ df -h | grep lvm
/dev/mapper/lvm--raid0-lvm0
                      7.2T  6.6T  232G  97% /export/lvm-raid0

$ truncate -s 500M 500MB.file

### AFTER
$ df -h | grep lvm
/dev/mapper/lvm--raid0-lvm0
                      7.2T  6.6T  232G  97% /export/lvm-raid0

因此,在执行此操作时,您将推迟物理分配,直到文件被访问为止。如果将此文件映射到内存,则可能无法获得预期的性能。

但这仍然是一个需要知道的有用命令。例如,当使用文件进行基准传输时,指定的文件大小仍然会被移动。

$ rsync -aHAxvP --numeric-ids --delete --info=progress2 \
       root@mulder.bub.lan:/export/lvm-raid0/500MB.file \
       /export/raid1/
receiving incremental file list
500MB.file
    524,288,000 100%   41.40MB/s    0:00:12 (xfr#1, to-chk=0/1)

sent 30 bytes  received 524,352,082 bytes  38,840,897.19 bytes/sec
total size is 524,288,000  speedup is 1.00

创建一个1gb的文件:

dd if=/dev/zero of=filename bs=1G count=1

你可以使用https://github.com/flew-software/trash-dump 您可以创建任意大小和随机数据的文件

这里有一个命令,你可以在安装垃圾转储(创建一个1GB的文件)后运行

$ trash-dump --filename="huge" --seed=1232 --noBytes=1000000000

顺便说一下,这是我创造的

这是一个常见的问题——尤其是在当今的虚拟环境中。不幸的是,答案并不像人们想象的那么简单。

Dd显然是第一选择,但Dd本质上是一个副本,它迫使您写入每个数据块(因此,初始化文件内容)…初始化占用了大量的I/O时间。(想要花更长的时间?使用/dev/random而不是/dev/zero!然后你将使用CPU以及I/O时间!)最后,dd是一个糟糕的选择(尽管它本质上是VM“create”gui使用的默认值)。例句:

dd if=/dev/zero of=./gentoo_root.img bs=4k iflag=fullblock,count_bytes count=10G

truncate is another choice -- and is likely the fastest... But that is because it creates a "sparse file". Essentially, a sparse file is a section of disk that has a lot of the same data, and the underlying filesystem "cheats" by not really storing all of the data, but just "pretending" that it's all there. Thus, when you use truncate to create a 20 GB drive for your VM, the filesystem doesn't actually allocate 20 GB, but it cheats and says that there are 20 GB of zeros there, even though as little as one track on the disk may actually (really) be in use. E.g.:

 truncate -s 10G gentoo_root.img

fallocate是用于虚拟机磁盘分配的最终——也是最好的——选择,因为它本质上是“保留”(或“分配”您正在寻找的所有空间,但它不需要写任何东西。因此,当您使用fallocate创建一个20 GB的虚拟驱动器空间时,您实际上得到了一个20 GB的文件(不是一个“稀疏文件”,并且您不会费心向其写入任何内容—这意味着几乎任何东西都可以在其中—有点像一个全新的磁盘!)例如:

fallocate -l 10G gentoo_root.img