Sometimes we need a large file with any content on Linux. This may be needs for benchmarking and testing purpose. Here are some ways to create large file on Linux.
Using dd
To create a 1GB file filled will nulls (zeros) using 1MB buffer
$ dd if=/dev/zero of=file.out bs=1M count=1024
To see the time taken, we can run it using time
$ time dd if=/dev/zero of=file1.out bs=1M count=1024 1024+0 records in 1024+0 records out 1073741824 bytes (1.1 GB) copied, 14.5134 s, 74.0 MB/s real 0m14.546s user 0m0.000s sys 0m0.795s
Note that instead of /dev/zero
we can also use /dev/urandom
top copy random numbers in data. This will be slower though.
Using fallocate
faalocate does not have to read device like dd and works much faster.
$ time fallocate -l 1G file2.out real 0m0.001s user 0m0.000s sys 0m0.001s
Using truncate
Truncate is also very fast but it creates sparse file. Command du will report it as 0 disk space used file unless we use –apparent-size option.
$ time truncate -s 2G file3.out $ du file3.out 0 $ du --apparent-size file3.out 1048576 file3.out