Sometimes when you work with applications that need huge files (of those that you do not have at the precise moment in which you work), it's quite easier to create it dinamically instead of downloading it or transferring it from a portable hard disk. In a linux environment this is quite easy to do and there are multiple ways to do it. In this article we will explain you how to do this in 4 different ways.
The files created by the
truncate instructions aren't fit to work with text editors and so, they're just mean to be binary files that occupy X space on your disk. The
dd commands will use IO overhead.
Having said that, let's get started !
A. Using fallocate
The first option to create a file with a fixed size, is using the fallocate command. Fallocate is used to manipulate the allocated disk space for a file, either to deallocate or preallocate it. For filesystems which support the fallocate system call, preallocation is done quickly by allocating blocks and marking them as uninitialized, requiring no IO to the data blocks. The command receives in bytes the size of the file that should be generated, for example to generate a file of 10MB, the following command should do the trick:
fallocate -l 1000000 file.txt
You could as well use a short syntax to write in Megabytes like this, for example the following command would generate a 10Mb file:
fallocate -l $((10*1024*1024)) file.txt
This option doesn't use input/output overhead, the space will be allocated immediately.
B. Using truncate
The other option to create the files is the truncate command. The truncate command allows you to shrink or extend the size of a file to a specific size, it receives as argument the filepath that is created if it doesn't exist. If a FILE is larger than the specified size, the extra data is lost. If the is shorter, it is extended and the extended part (hole) reads as zero bytes.
The following command would create a 10MB file:
truncate -s 10M file.txt
This creates a file full of null bytes. If the file already exists and is smaller, its size is extended to the requested size with null bytes. Otherwise if its larger, it is truncated to the requested size. The result file of this command will be a Sparse file.
C. Using dd
dd is a command-line utility for Unix and Unix-like operating systems whose primary purpose is to convert and copy files. With the following command, you should be able to create a file with a custom size (replacing 10MB with the size of the file that you want to generate):
dd if=/dev/urandom of=ostechnix.txt bs=10MB count=1
The command will generate the following output:
1+0 records in 1+0 records out 10000000 bytes (10 MB, 9,5 MiB) copied, 0,280547 s, 35,6 MB/s
And the file should have the desired size.
D. Using head
The head command is a command-line utility for outputting the first part of files given to it via standard input. It writes results to standard output.
head -c 10MB /dev/urandom > file.txt
This command will create a non-sparse file full of null bytes.
Happy coding ❤️!