1. Overview

As Linux users, we frequently perform various operations on files. A common operation is to create a file of a certain size.

In this tutorial, we’ll discuss the various ways to achieve this.

2. Using the fallocate Command

fallocate is a simple command to allocate disk space by creating a file. Let’s create a file of 100 MiB:

$ fallocate -l 100M file1.txt
$ ls -lh file1.txt 
-rw-rw-r-- 1 groot groot 100M May 15 20:26 file1.txt

In this case, we’re using the -l argument to represent the length of the file in bytes.

The fallocate command also accepts sizes in human-readable formats like Kilobytes (K), Megabytes (M), and Gigabytes (G).

3. Using the truncate Command

The truncate command can extend or shrink the file to a given size. Let’s use it to create a file of 200 MiB:

$ truncate -s 200M file2.txt
$ ls -lh file2.txt
-rw-rw-r-- 1 groot groot 200M May 15 20:36 file2.txt

Here, we’re using the -s argument to represent the size of the file in bytes.

Note, if the file exists and it’s smaller than what is specified with the -s option, then the file size is increased to the requested size with ASCII NUL bytes. If the existing file is greater in size, then it’s truncated to the requested size.

4. Using the head and tail Commands

The head command can be used with the /dev/zero file to create a file filled with a set number of ASCII NUL characters:

$ head --bytes 300K /dev/zero > file3.txt
$ ls -lh file3.txt
-rw-rw-r-- 1 groot groot 300K May 15 20:47 file3.txt

In this case, the –bytes option represents the desired file size in bytes.

Similarly, the tail command can be used in the same way:

$ tail --bytes 1G /dev/zero > file4.txt
$ ls -lh file4.txt
-rw-rw-r-- 1 groot groot 1.0G May 15 20:52 file4.txt

5. Using the dd Command

The dd command converts and copies the file. Let’s use dd to create a file of 10 MiB:

$ dd if=/dev/zero of=file5.txt bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0.0387031 s, 271 MB/s
$ ls -lh file5.txt
-rw-rw-r-- 1 groot groot 10M May 15 20:58 file5.txt

Let’s take a look at the arguments:

  • if represents input file
  • of represents output file
  • bs represents block size in bytes
  • count represents the number of blocks to be copied

6. Conclusion

In this tutorial, we discussed five practical methods to create a file of a certain size. These commands can be used in day-to-day life while working with the Linux system.

Comments are closed on this article!