Create a large file on Linux

How can I quickly create a large file on Linux?

Normally dd command creates a large file but it reads from /dev/zero and then writes to drive which can take a long time to create a large file.

Create a large file on Linux
People ask why I used this pic, this is not the file I am talking about. But infact file is just a place to store some information, It can be hardcopy of softcopy 🙂

To overcome the speed issue you can use fallocate command which will create a large file for you in no time.

However before we see how to create a large file, let’s see in what scenarios you will need a large file.

Testing this is the only reason you will ever need to create a large file. Most people, need large files to check the upload and download speed of the server. In some cases, people also create a large file to convert it to swap.

How to create a large file?

Method 1:

The first method is using the fallocate command.

The below command will create a 10GB file in no time. You can modify it as per your needs

$ fallocate -l 10GB /justgeek
[[email protected]/]# du -sh justgeek
9.4G    justgeek

There are multiple other options that you can use with the fallocate command, you can try the help command to see all those options.

[[email protected]/]# fallocate --help

 fallocate [options] <filename>

Preallocate space to, or deallocate space from a file.

 -c, --collapse-range remove a range from the file
 -d, --dig-holes      detect zeroes and replace with holes
 -i, --insert-range   insert a hole at range, shifting existing data
 -l, --length <num>   length for range operations, in bytes
 -n, --keep-size      maintain the apparent size of the file
 -o, --offset <num>   offset for range operations, in bytes
 -p, --punch-hole     replace a range with a hole (implies -n)
 -z, --zero-range     zero and ensure allocation of a range
 -x, --posix          use posix_fallocate(3) instead of fallocate(2)
 -v, --verbose        verbose mode

 -h, --help           display this help
 -V, --version        display version

For more details see fallocate(1).

Method 2

If you do not wish to use the fallocate command for some reason, you can always go ahead with the DD command to create the large file on the server.

DD command basically copies the data from /dev/zero which is at the times slower, as I mentioned earlier.

$ dd if=/dev/zero of=justgeek.txt count=1024 bs=1024
1024+0 records in
1024+0 records out
1048576 bytes (1.0MB) copied, 0.012096 seconds, 82.7MB/s

The arguments are as follows:

  • if is the input source
  • of is the output file
  • count is the number of times to repeat a copy
  • bs is the size of the chunk that is copied on each step. (bs stands for block size)

Now we’ve created a file that is 1MB. In dd, you specify the size by multiplying the block size, 1024 (or 1k), by the count (1024).

So for an example you wanted to create a 1GB file you will do 1024 * 1024 i.e. 1048576

dd if=/dev/zero of=justgeek.txt count=1048576 bs=1024
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.0GB) copied, 19.204383 seconds, 53.3MB/s

However, if you run cat on the justgeek.txt file you will see it’s empty.

ls -ltrah justgeek.txt
-rw-r--r--    1 justgeek UsersGrp    1.0G Jun 12 01:40 justgeek.txt

When you run the DD command, it is actually copying the contains /dev/zero which is actually generating a series of null characters.

And just in case you want to create a file using random characters, instead of null characters then you can use /dev/urandom instead.

$ dd if=/dev/urandom of=justgeek.txt count=1048576 bs=1024
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.0GB) copied, 19.974795 seconds, 51.3MB/s

As you saw in the post above, It’s easy to create a large file on Linux. Which you will use for your testing purpose.

Spread the love

Leave a Comment