Create a Large File on Linux

How to create a large file on Linux, Unix or BSD OS from command line?

There are several commands that can help you create a large file on Linux OS but the most used are dd and fallocate.

First of all we’ll need to check and make sure that we have enough disk space for creating this file. df -h will help us here and the output should be similar to this:

[root@web ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 30G 2.8G 26G 10% /

1. fallocate

fallocate is used to preallocate blocks to a file. This is much faster than creating a file by filling it with zeros which means that dd or truncate are way slower than fallocate. The syntax is quite simple and straight forward: fallocate -l SIZE PATH/NAME:

[root@web ~]# fallocate -l 1G 1GB_file.img
[root@web ~]# ls -lah 1GB_file.img
-rw-r--r-- 1 root root 1.0G Feb 17 14:11 1GB_file.img

This will create a file called 1GB_file.img with 1GB in size.

Other examples:

  • fallocate -l 100M 100M_file.img – This will create 100MB file
  • fallocate -l 50M 50M_file.img – This will create 50MB file
  • 2. dd

    dd is an utility that can be used to convert and copy files. The syntax is quite simple, and dd if=/PATH/INPUT of=/PATH/OUTPUT should do it. On Linux special device files(such as /dev/zero and /dev/random) show up as regular files which means dd can also read and/or write from/to them. The following command will create a 1GB file:

    [root@web ~]# dd if=/dev/zero of=1G.bin bs=1G count=1
    1+0 records in
    1+0 records out
    1073741824 bytes (1.1 GB) copied, 26.838 s, 40.0 MB/s