In this article, we will see how to create a large file in Linux using 4 simple and easy methods. There could be many situations which requires you to create a large file in Linux. One of the possible situation could be to test the disk space monitoring tools if working properly or not. If you don't have much data in your System then it will be difficult to test the disk monitoring functionality. So in cases like this you can quickly create a large dummy file to test your disk monitoring application.
Similarly, there might be some other use case that requires you to create a large file in your Linux System. So for all those cases, we will see some of the easily available tools that can accomplish this task with ease.
How to Create a Large File in Linux
Method 1: Using fallocate command
This is probably the quickest way to create a large file. You can use
fallocate -l <size> <file_name> syntax to create a large file. Here we are creating an 8G file name largefile using
fallocate -l 8G largefile command. You can verify the size of the file by using
du -sh largefile command as shown below. Check more options on its Man Page.
root@localhost:~# fallocate -l 8G largefile root@localhost:~# du -sh largefile 8.1G largefile
Method 2: Using dd command
This is one of the famous method in Linux which is probably in use from last couple of years. Using
dd command, the blocks from the input file will be written to the output file. You might have used this command to backup your hard disk data. Here we are increasing the size of output file called
largefile by writing the blocks from input
/dev/zero file to a count of
8G and using the block size of
4k. However it is important to note here that this command is slower than fallocate and truncate command as it takes lot of time in I/O operations.
root@localhost:~# dd if=/dev/zero of=largefile bs=4k iflag=fullblock,count_bytes count=8G 2097152+0 records in 2097152+0 records out 8589934592 bytes (8.6 GB, 8.0 GiB) copied, 44.8193 s, 192 MB/s
You can verify the size of the file by using
du -sh largefile command as shown below.
root@localhost:~# du -sh largefile 8.1G largefile
Method 3: Using truncate command
Another very useful tool that you can think of using is truncate command. You can use
truncate -s <size> <file_name> syntax to create a file. Here we are creating a file called largefile of size 8G using truncate -s 8G largefile command as shown below. You can verify the size of the file by using
du -sh --apparent-size largefile command.
It is important to note here that truncate always create files of apparent size which means it actually does not store in any actual data but gives an impression to OS that it is storing and hence the file size is increasing.
root@localhost:~# truncate -s 8G largefile root@localhost:~# du -sh --apparent-size largefile 8.0G largefile
Method 4: Using seq command
This is probably one of the slowest method to use. Using this method, you can fill the file with long sequence which will ultimately increase the size to large extent. Depending on the size you want to create, you can use the sequence accordingly - the larger the sequence, bigger is the size. For example, here we are writing sequence
seq 1000000000 >> largefile command. This will create a file of size around
9.3G but the process will be much slower than the other ones. Check more options on Man Page.
root@localhost:~# seq 1000000000 >> largefile root@localhost:~# du -sh largefile 9.3G largefile