In this article we will learn to Create Files of Custom Size in Linux. You can create files with your own specific size in linux using different methods. It is very useful when you perform testing of some sorts and you need files with a specific size.
We will not be using any external commands. Built in commands will be used to perform this task so that you may not download any external package.
Create Custom size files using truncate:
First, we will use truncate command to create a file with size 5MB using the following command:
truncate -s 5M unixcop.txt
As you can see in the above image a file having name unixcop.txt of size 5MB is present after running the above mentioned command.
To further study the functionality of truncate command use can use help of your linux system using the below mentioned command:
Creating files using Fallocate:
Other than truncate we can also use fallocate command to create files with custom size. There is one limitation that you have to specify the size of file in BYTES only. It will not take any other unit of file size.
Use the following command to create file of 1MB. Please note that 1MB = (1*1024*1024) BYTES.
fallocate -l 1048576 unixcop.txt
As you can see in the above image unixcop.txt files having size of 1MB is created.
Similarly, you can view the functionality of this command in detail using the help in linux:
Create Files using Head command:
We often use head command to view / print start of the file in CLI. Today, we will use head command to create custom size files in linux. Use the following command to create a file of 10MB:
head -c 5MB /dev/urandom > unixcop.txt
You can explore head command using the help feature in linux.
Create specific size files using dd command:
There is another command dd which is normally used to convert or copy a file bit we will use it to create files of custom size in linux. Use the following command to create file of 15MB.
dd if=/dev/urandom of=unixcop.txt bs=15MB count=1
As you can see in the above image a unixcop.txt file is present having size of 15MB.
We have now multiple ways to Create Files of Custom Size in Linux. You can use each of them according to your need.