如何在Linux (Red Hat Linux)系统中快速创建大文件?
dd可以完成这项工作,但是从/dev/zero读取并写入驱动器可能会花费很长时间,因为您需要一个几百gb大小的文件进行测试……如果你需要重复这样做,时间就会增加。
我不关心文件的内容,我只想快速创建它。如何做到这一点呢?
使用稀疏文件不能解决这个问题。我需要为文件分配磁盘空间。
如何在Linux (Red Hat Linux)系统中快速创建大文件?
dd可以完成这项工作,但是从/dev/zero读取并写入驱动器可能会花费很长时间,因为您需要一个几百gb大小的文件进行测试……如果你需要重复这样做,时间就会增加。
我不关心文件的内容,我只想快速创建它。如何做到这一点呢?
使用稀疏文件不能解决这个问题。我需要为文件分配磁盘空间。
当前回答
这是我在以下约束条件下所能做到的最快速度(并不快):
大文件的目标是填满磁盘,因此不能压缩。 使用ext3文件系统。(fallocate不可用)
这是它的要点……
// include stdlib.h, stdio.h, and stdint.h
int32_t buf[256]; // Block size.
for (int i = 0; i < 256; ++i)
{
buf[i] = rand(); // random to be non-compressible.
}
FILE* file = fopen("/file/on/your/system", "wb");
int blocksToWrite = 1024 * 1024; // 1 GB
for (int i = 0; i < blocksToWrite; ++i)
{
fwrite(buf, sizeof(int32_t), 256, file);
}
在我们的情况下,这是一个嵌入式linux系统,这工作得很好,但更喜欢更快的东西。
供您参考,命令dd if=/dev/urandom of=outputfile bs=1024 count = XX速度太慢,无法使用。
其他回答
你可以使用https://github.com/flew-software/trash-dump 您可以创建任意大小和随机数据的文件
这里有一个命令,你可以在安装垃圾转储(创建一个1GB的文件)后运行
$ trash-dump --filename="huge" --seed=1232 --noBytes=1000000000
顺便说一下,这是我创造的
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
我不认为你会比dd快很多,瓶颈是磁盘;无论你怎么做,写入几百GB的数据都将花费很长时间。
But here's a possibility that might work for your application. If you don't care about the contents of the file, how about creating a "virtual" file whose contents are the dynamic output of a program? Instead of open()ing the file, use popen() to open a pipe to an external program. The external program generates data whenever it's needed. Once the pipe is open, it acts just like a regular file in that the program that opened the pipe can fseek(), rewind(), etc. You'll need to use pclose() instead of close() when you're done with the pipe.
如果你的应用程序需要文件有一定的大小,它将由外部程序来跟踪它在“文件”中的位置,并在到达“结束”时发送一个eof。
不义之词:OTFFS提供了一个文件系统,可以提供任意大的文件系统。Exabytes是当前的限制)文件生成的内容。它只支持linux,使用纯C语言,并且处于早期alpha版本。
见https://github.com/s5k6/otffs。
我对Linux不太了解,但这是我多年前在DC共享上编写的伪造大文件的C代码。
#include < stdio.h >
#include < stdlib.h >
int main() {
int i;
FILE *fp;
fp=fopen("bigfakefile.txt","w");
for(i=0;i<(1024*1024);i++) {
fseek(fp,(1024*1024),SEEK_CUR);
fprintf(fp,"C");
}
}