Post

Create very large size files | LinuxGist

This article will provide details on how we can create very large size files quickly in Linux. This will be useful for certain test scenerios where we have to quickly fill the disk space.

Introduction

Creating very large size files in Linux quickly can be done using several methods. Here are some common approaches:

Method 1: Using dd Command

The dd command is a versatile tool that can be used to create files of any size.

1
dd if=/dev/zero of=largefile bs=1M count=1024
  • if=/dev/zero: This specifies the input file as /dev/zero, which is a special file that provides an infinite stream of zero bytes.
  • of=largefile: This specifies the output file name (largefile).
  • bs=1M: This sets the block size to 1 megabyte (you can adjust this value based on your needs).
  • count=1024: This specifies that you want to copy 1024 blocks of 1 megabyte each, resulting in a file of approximately 1 gigabyte.

Method 2: Using /dev/urandom for Random Data

If you need a file filled with random data, you can use /dev/urandom.

1
dd if=/dev/urandom of=randomfile bs=1M count=1024
  • if=/dev/urandom: This specifies the input file as /dev/urandom, which provides an infinite stream of random bytes.
  • of=randomfile: This specifies the output file name (randomfile).
  • bs=1M: This sets the block size to 1 megabyte.
  • count=1024: This specifies that you want to copy 1024 blocks of 1 megabyte each, resulting in a file of approximately 1 gigabyte.

Method 3: Using fallocate Command

The fallocate command is designed for creating files with preallocated disk space and is generally faster than using dd.

1
fallocate -l 5G largefile
  • -l 5G: This specifies the size of the file as 5 gigabyte. You can adjust this value based on your needs.
  • largefile: This specifies the output file name.

Method 4: Using truncate Command

The truncate command can also be used to create files of a specific size.

1
truncate -s 2G largefile
  • -s 2G: This specifies the size of the file as 2 gigabyte. You can adjust this value based on your needs.
  • largefile: This specifies the output file name.

Examples

Creating a 5GB File Using dd

1
dd if=/dev/zero of=largefile bs=1M count=5120

Creating a 10GB File Using fallocate

1
fallocate -l 10G largefile

Creating a 2GB File Using truncate

1
truncate -s 2G largefile

Summary

These methods provide several ways to create large files in Linux for testing purposes. The choice of method depends on your specific needs, such as the size of the file and whether you require random data.

This post is licensed under CC BY 4.0 by the author.