Common file creation methods are as follows:

  1. touch
  2. vi(m)
  3. tee
  4. >>>

However, all of these are only suitable for creating small text files. In some cases, for testing purposes, you may need to quickly create a very large file, which may be hundreds of gigabytes. To use the above commands, you may have to wait all day, which is very inefficient.

Here are a few of my favorite methods

1. dd

The dd command reads data from standard input or a specified file and copies it to a new file.

$ dd if=/dev/zero of=big_file count=10 bs=1GCopy the code

Using the time command, you can calculate how long it takes to create a 10GB file.

$ time dd if=/dev/zero of=big_file count=10 bs=1G 10+0 records in 10+0 records out 10737418240 bytes (11 GB) copied, 7.93627s, 1.4GB /s real 0m7.941s user 0m0.000s sys 0M7.935sCopy the code

It took almost eight seconds

$ ls -lh big_file 
-rw-r--r-- 1 root root 10G Jun  2 10:57 big_fileCopy the code

2. fallocate

The fallocate command can preallocate physical space for a file. -l Indicates the size of subsequent space. The default unit is byte. You can also specify the unit by following k, m, g, T, P, and E, representing KB, MB, GB, TB, PB, and EB respectively.

$ fallocate -l 10G big_fileCopy the code

Using the time command, you can calculate how long it takes to create a 10GB file.

$time fallocate -L 10G big_file real 0m0.002s user 0m0.000s sys 0m0.001sCopy the code

It took 0.001 seconds.

$ ls -lh big_file 
-rw-r--r-- 1 root root 10G Jun  2 11:01 big_fileCopy the code

Run the du command to check whether a 10GB file has been created. Judging by the results, yes.

$ du -sh big_file
10G    big_fileCopy the code

3. truncate

The truncate command reduces or expands a file to a specified size. Use the -s parameter to set the file size.

$ truncate -s 10G big_fileCopy the code

Using the time command, you can calculate how long it takes to create a 10GB file.

$time TRUNCate -S 10G BIG_file real 0M0.001s user 0M0.000s sys 0M0.001sCopy the code

0.001 seconds.

However, unlike fallocate, allocate creates a true size file, while TRUNCate does not.

The results of the ls and du commands are different.

$ ls -lh big_file 
-rw-r--r-- 1 root root 10G Jun  2 11:11 big_file

$ du -sh big_file 
0    big_fileCopy the code

Truncate specifies the size of a file. If the file does not exist, the truncate file will be created. If the specified file size is smaller than the original size, the content is lost.

This command specifies a virtual file size. It’s just the size that shows up. If you specify a very large file. In fact, server free space does not decrease.


The above are several methods I commonly use to create large files, which can be selected according to different scenarios. I generally use Fallocate. The speed is fast enough, but it also takes up disk space, which is in line with real test scenarios.