How to auto-create a 1 GB or large file in Linux

What would you do if someone asked you to create a file size of 1 GB or more in Linux? Most likely, you will use compression utilities like tar or gzip to create a file size of around 1 GB or more, which is not optimal at all, because first, you will collect data and then compress, even though you will not be sure about the size of the file, right?

Although you can use multiple utilities to achieve this task without any effort, just run a single command and your large file is automatically served to your system.

The command which you are going to use is:

  • dd command
  • fallocate command
  • truncate command

First and foremost, we will understand the purpose of creating a large file in Linux.

Most users preferred to read How to Gzip 100GB+ Files Faster with High Compression

Why to create a large file in Linux

There are a number of reasons why you need a large file. One may be looking to test the functionality of the operating system, or want to check the write speed of the disk, or maybe you want to create a block device for loop devices

Whatever the reasons may be, you can create a sample file size of 1 Mb to 1 GB or more with the above-mentioned command without aggregating data from various sources

How to create large file using dd command

The most preferable command to use to create large files is the dd command, which is an all-in-one tool for converting and copying images from the respective source to destination, even though you can specify which type of file you want to copy. 

Let us create a large file with a size of 1 GB in my current path using the below command:

$ dd if=/dev/zero of=test.img bs=1 count=0 seek=1G
  • dd – command to convert copy image
  • if/dev/zero is character devices is responsible for null data
  • of – your sample file name or location
  • bs – specify just 1 byte
  • count – number of blocks to copy
  • seek – specify the file size
Output:

0+0 records in
0+0 records out
0 bytes copied, 0.000733671 s, 0.0 kB/s

Oh wow! It’s blazing fast. Maybe it’s because it’s just 1 GB of file. Let me run the same command and change the size to 100 GB.

$ dd if=/dev/zero of=DUMMY-100GB.img bs=1 count=0 seek=100G

Output:
0+0 records in
0+0 records out
0 bytes copied, 0.000979038 s, 0.0 kB/s

It took me barely a second to create a 100GB file. And I can’t believe that a 100GB file can exist on my system because I already have space constraints, so how is it possible that the dd command can create a 100GB file?

Let me see if the “DUMMY-100GB.img” file exists on my system.

$ ls -lh DUMMY-100GB.img
-rw-r--r-- 1 trendoceans trendoceans 100G Mar  8 12:49 DUMMY-100GB.img
Create 100gb of dummy file in linux
DUMMY-FILE.IMG SIZE

How does it happen? If you check the output, it shows 0 bytes copied, which means the dd command has just created an empty file without copying any data. 

$ dd if=/dev/zero of=DUMMY-100GB.img bs=1 count=0 seek=100G

Output:
0+0 records in
0+0 records out
0 bytes copied, 0.000979038 s, 0.0 kB/s

If you want a dummy file to hold any data, then run the following command:

$ dd if=/dev/zero of=test.img bs=1024M count=1   
$ dd if=/dev/urandom of=test.img bs=1024M count=1
Output:

1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 3.56116 s, 302 MB/s

This process will be slow compared to the above because now the file is getting copied onto the test.img file.

How to create large file using fallocate command

The above method can be overwhelming if you have never used the dd command, but it is quite intuitive to learn. Nevertheless, you can use another tool, which is fallocate. It is used to manipulate disk space for a file.

The basic syntax of fallocate to create a file is as follows:

$ fallocate -l SIZE-OF-FILE /SPECIFY/PATH.img

The -l or --length flag is used to specify the size. You can use suffixes like k, m, g, t, p, e, and y or you can use KiB, MiB, GiB, etc.

If I want to prelocate or create a dummy file that holds data of 5 GB, then how to use the fallocate command? It’s pretty straightforward. Just run the below command:

$ fallocate -l 5G test_b.img 

Let me verify file existence using ls and du command is used to find disk usage with -h for human readable size:

$ ls -lh test_b.img
$ du -h test.img  

Output:
-rw-r--r-- 1 trendoceans trendoceans 5.0G Mar  8 13:34 test_b.img
5.1G    test.img

How to create large file using truncate command

You can also use the truncate command to create a large file in Linux, which is similar to the dd command. It creates a sparse file and allocates the specified space in your system, and the last thing is that space is only allocated when you write data to the file.

The basic syntax of truncate is as follows:

$ truncate -s [SIZE-OF-FILE][/SPECIFY/PATH.img]

The -s or --size flag uses to specify the size you can use suffixes like k, m, g, t, p, e, and y or you can use KiB, MiB, GiB, etc.

To create a large file named test.img with a size of 1 GB, then pass the below code:

$ truncate -s 1G test.img

Which is the fastest way to create a large file in Linux

Now you know all three methods for creating a large file in Linux without aggregating data from various sources. If you want to check which command is optimal in terms of speed, then you can run any following command with a time command to check the speed of the newly created files.

$ time dd if=/dev/zero of=test_a.img bs=1 count=0 seek=5G
$ time fallocate -l 5G test_b.img                     
$ time truncate -s 5G test_c.img

The output of all three commands.

************** DD COMMAND OUTPUT *********************************************
0+0 records in
0+0 records out
0 bytes copied, 0.000733671 s, 0.0 kB/s
dd if=/dev/zero of=test_a.img bs=1 count=0 seek=5G  0.00s user 0.00s system 53% cpu 0.004 total

************** FALLOCATE COMMAND OUTPUT *************************************
fallocate -l 5G test_b.img  0.00s user 0.05s system 81% cpu 0.056 total

************** TRUNCATE COMMAND OUTPUT **************************************
truncate -s 5G test_c.img  0.00s user 0.00s system 49% cpu 0.008 total

If you are not interested in having dummy data, then I would prefer you to use the truncate command over the dd command. A fallocate operation is comparatively faster than the dd command because it’s actually allocated space and copies the data to the respective file. 

Wrap up

That’s all to auto-create a 1 GB or large file in Linux, even you have experienced how to create 100 GB of file in a second.

Most users preferred to read Bash Keyboard Shortcuts for the pro!

This Post Has One Comment

  1. castaway

    I would use head.

    yes 000000 | head -c1000000000 > t.txt

Leave a Reply