How to Create Large Files in Linux

Creating files in Linux is something we do all the time. Mostly you create an empty text file with the touch command and then add content to it.

But what about creating new files of a certain size?

When you are troubleshooting something or want to test in some particular scenario, you may require large files bigger than a certain size. Let's say 500 MB or 2 GB.

Now, you cannot create an empty file and then start writing garbage text in it. You can never be able to create a file of 1 GB in size this way.

Thankfully, you don't have to manually create large text files. There are various commands that allow you to create large files of predefined size. They won't have desired text. Just some random garbage but you'll get the file of your desired size.

Let me show how to do that.

Creating large files using the dd command

The dd command is used for copying and converting files. Its most common use can be found in creating live Linux USBs.

Let's say you want to create a text file named testfile.img of 2 GB size. Here's what you can do:

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

Depending on the size of the file, the command will take some time to complete.

Here, you created a file of 2 GB in size that has a single block (count 1) of size (bs, block size) 2 G. The file contains the NULL characters (/dev/zero).

You can change the block size and count of blocks as you per your need. For example, you could have used 1M as bs with 1024 as count to get file of 1024 Mb. You can mix and match as you like.'

💡
Since the file only contains NULL, you cannot count lines in it. If you want some text content instead of NULL, you can use /dev/random as the input in the dd command.

Using the truncate command to create huge files

The truncate command reduces or increases the size of each FILE to the desired size.

Extra data is lost if a FILE is bigger than the required size. If a FILE is too short, it is expanded, and the extra portion (hole) is accessed as zero bytes.

Let's use the truncare command to create file of 2 GB in size.

truncate -s 2G testfile.img

You will see no output from the above command, however, the resultant file can be seen using the ls command:

ls -lh testfile.img
💡
By default, the truncate command will create new files if the requested output file does not already exist. You can use the option -c to avoid creation of new files.

Using the fallocate command to create huge files

The fallocate command is my recommended way for creating a large file because it is the fastest.

To create a file of 1 GB, use it like this:

fallocate -l 1G testfile1.img

Now check the output file:

ls -lh testfile1.img

It is far quicker to use fallocate than to create a file by populating it with zeroes.

Conclusion

The files created by the dd and truncate are sparse files. In the computer world, a sparse file is a special file that tries to utilize the space on a file system in a very efficient manner when the blocks assigned to a file are mainly empty.

Sparse files have varying apparent file sizes (the largest size to which they can expand) and true file sizes (how much space is allocated for data on disk).

You can see the apparent size and the true size with the du command:

This is why I prefer using fallocate command. It is faster and it does not create sparse files.