A while ago, I had setup a local file server using PSiTransfer. While using my file server, I wanted to test the upload limit, maximum upload size, and download speed of the files/folders. For that purpose, I needed different size files. I have various size files in my hard drive. But, I want a file to be exactly 100 MB in size. After a bit of web search, I found out how to create files of a certain size in Linux and Unix-like systems.
Create Files Of A Certain Size In Linux
There are few ways to create files with given size. I will show them all with practical examples.
All commands mentioned in this guide are part of GNU coreutils, so you don't have to install them. These commands comes pre-installed by default.
1. Create files of a certain size using truncate command
To create a specific size file, for example 5 MB, using truncate
command, run:
$ truncate -s 5M ostechnix.txt
The above command will create a file called ostechnix.txt
with size exactly 5MB
.
For more details about this command, refer truncate
man pages.
$ man truncate
2. Create files of a particular size using fallocate command
The another command to create a particular size file is fallocate
. Please note that you can only specify file sizes in bytes using fallocate
command. To calculate the size for a specific file, for example 5MB
, you need to do - 510241024=5242880
. Clear? Good!
Now let us create a file of size 5MB using command:
$ fallocate -l 5242880 ostechnix.txt
As Luc Van Rompaey suggested in the comment section, with the bash shell, you can do inline arithmetic, so you won’t have to calculate how many bytes go into 5 MiB
beforehand.
So, we can use this as shown below:
$ fallocate -l $((5*1024*1024)) ostechnix.txt
For more details about this command, I suggest you to go through the fallocate
man pages.
$ man fallocate
3. Create files of a certain size using head command
We use head
command to output the first part of files, right? Well, we can use this command to create a file of certain size too.
To create a file with 5 MB
in size using head
command, run:
$ head -c 5MB /dev/urandom > ostechnix.txt
The above command will create 5MB
size file filled with random data. You can also create the file with 0s
as shown below.
$ head -c 5MB /dev/zero > ostechnix.txt
Refer man pages for further details about head
command.
$ man head
4. Create files of a certain size using dd command
We already knew we can convert and copy a file using dd
command. We also use dd command to create a bootable disk. However, we can use this command to create files of particular size as well.
To create a file with size 5MB, run:
$ dd if=/dev/urandom of=ostechnix.txt bs=5MB count=1
Sample output:
1+0 records in 1+0 records out 5000000 bytes (5.0 MB, 4.8 MiB) copied, 0.0402477 s, 124 MB/s
The command will create ostechnix.txt
file of size 5MB
filed with some random data.
To create a file filled with 0s
, you can use:
$ dd if=/dev/zero of=ostechnix.txt bs=5MB count=1
As usual, for details about this command, refer the dd
command man pages.
$ man dd
And, that's all. You know now how to create a file with certain size in Linux. As you can see in the above examples, generating files of specific size is no big deal. Hope this helps.
6 comments
Just a sidenote: With the bash shell, you can do inline arithmetic, so you won’t have to calculate how many bytes go into 5 MiB beforehand.
Just do:
fallocate -l $(( 5 * 1024 * 1024 )) ostechnix.txt
Cool. Thank you. I didn’t know this.
I think its good to mention that the first 2 command only allocate file with the given size in the file system, which is means no IO overhead.
but the other two create an empty file and extending it by writing data in it, which means LOTS of IO overhead
By the way thanks for good and useful article.
Thank you for your clarification.
Thanks, is very useful info
It also worth noting that truncate creates a file that looks like that size, but doesn’t allocate space. If you want to create a fake block device or a swap file you can’t use it, as the kernel will tell you “it has holes”.