Generating files of a specific size can be useful for testing purposes. There are several different commands in Linux that can create files of certain sizes. Check out the commands and examples below to learn how.
Generate file of specific size with truncate
The truncate command may be the perfect tool for the job, as the command’s only purpose is to shrink or extend the size of a file to a specific size. You can use this to create new files, or manipulate the size of existing files.
Be careful, since you will lose data if you shrink the size of an existing file.
Example 1. Create a 10 MB file with truncate
command.
$ truncate -s 10M file.txt
Example 2. Create a 2 GB file with truncate
command.
$ truncate -s 2G file.txt
Example 3. The truncate
command is extremely fast, even when creating huge files. You just need the hard drive space to accommodate them. Create a 1 TB file with truncate
command.
$ truncate -s 1T file.txt
Example 4. Use a Bash for
loop to create multiple files of a certain size with truncate
. For example, to generate 10 files of 1 GB each:
$ for i in {1..10}; do truncate -s 1G file$i.txt; done
Generate file of specific size with dd
The dd command is a great tool for making raw copies of data. We can also use it to generate files of a specific size by specifying random input with /dev/urandom
, and a file size of our choosing.
Example 1. Create a 10 MB file filled with random data using dd
command.
$ dd if=/dev/urandom of=file.txt bs=10MB count=1
Example 2. Create a 2 GB file filled with zeros using dd
command with /dev/zero
.
$ dd if=/dev/zero of=file.txt bs=2GB count=1
Example 3. Use a Bash for
loop to quickly create multiple files of a certain size with dd
. This line will create 10 files of 1 GB each.
$ for i in {1..10}; do dd if=/dev/zero of=test$i bs=1G count=1; done
Generate file of specific size with head
Although head is ordinarily used to read the beginning of files, the -c
option can be used to read a certain amount of data from a file. Combined with files such as /dev/urandom
, we can effectively create a file of any size we choose.
Example 1. Create a 10 MB file filled with random data using head
command.
$ head -c 10MB /dev/urandom > file.txt
Example 2. Create a 2 GB file filled with zeros using head
command with /dev/zero
.
$ head -c 2GB /dev/zero > file.txt
Generate file of specific size with yes
It would be hard to get a precise file size with the yes command, but it can still be used to rapidly increase the size of a file.
Example 1. Create a new file with yes
command. After a few seconds, be sure to press Ctrl + C and check the size of your file. It will likely already be a few gigabytes.
$ yes > file.txt
Example 2. If you see the size of the file and want it bigger, just continue adding more data to the file using yes
and the >>
operator.
$ yes >> file.txt
Example 3. With the yes
command, we can fill our document with whatever repeated data we want. For example, let’s spam a file with “I love Linux!”
$ yes I love Linux! > file.txt ^C $ head -3 file.txt && wc -l file.txt I love Linux! I love Linux! I love Linux! 44471115 file.txt
After just a few seconds, our file has 44,471,115 lines that say “I love Linux!” and is 594 MB in size.