Summary of 4 ways to quickly create large files under Linux

1. Use the dd command to create large files

The dd command is used to copy and convert files, and its most common use is to create a live Linux USB. The dd command is to actually write to the hard disk. The speed at which the file is generated depends on the read and write speed of the hard disk. Depending on the size of the file, the command will take some time to complete.

Assuming we want to create a 2 GB text file called rumenz.img, we can do the following:

dd if=/dev/zero of=rumenz.img bs=2G count=1

We can change the block size and number of blocks according to our needs. For example, bs=1M and count=1024 can be used to get a 1024 Mb file.

2. Use the truncate command to create large files

This command reduces or expands the size of the specified file to the specified value. If the file specified by the parameter does not exist, the command will create this file.

If the size of a file is larger than the parameter specified, the excess part will be discarded; if a file is smaller than the parameter specified, then the file will be expanded, and the expanded part (hole) is when it is read Byte 0.

The truncate command shrinks or expands a file to the desired size, use the -s option to specify the size of the file.

Next, we use the truncare command to create a 2GB file.

truncate -s 2G rumenz.img

You can use the ls -lh rumenz.img command to view the generated file. By default, the truncate command will create a new file if the requested output file does not exist, we can use the -c option to avoid creating a new file.

3. Use the fallocate command to create large files

The fallocate command is probably one of the lesser known commands in Linux that you can use to create files. fallocate is used to pre-allocate blocks to files. For filesystems that support the fallocate system call, this can be done quickly by allocating blocks and marking them as uninitialized, so no I/O is required to the data blocks.

This is a much faster way of creating files than filling them with zeros, and large files can be created almost instantly without having to wait for any I/O operations to complete. The fallocate system call is supported on the following filesystems: btrfs, ext4, ocfs2, and xfs filesystems (since kernel version v2.6.31).

The fallocate command is my recommended method for creating large files, because it is the fastest way to create large files.

Suppose we want to create a 1 GB file, we can do the following:

fallocate -l 1G rumenz.img

The generated file can be viewed with ls -lh rumenz.img.

4. The code creates an accumulative number file


FILESIZE = 1*1024*1024*1024 #1G
print(int(FILESIZE/256))
file = open("./Fibonaci.txt",'w+')
for i in range(int(FILESIZE/256)):
    for j in range(256):
        file.write(str(j))

file.seek(0,0)
#print(file.read())
file.close()

5 Conclusion

The files created by dd and truncate are sparse files. In the computer world, a sparse file is a special kind of file that has a different apparent file size (the maximum size they can expand to) and a real file size (how much space is allocated for the data on disk).

The fallocate command does not create sparse files, and it is faster, which is why I recommend using fallocate to create large files.

Finally: I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free 【保证100%免费】

 

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

How to obtain the full set of information:

                              

Guess you like

Origin blog.csdn.net/NHB456789/article/details/131128272