A while ago, I 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 the file to be exactly 100 MB in size. After a bit of Google search and go through over some Linux forums, I found what I wanted. For those wondering, this brief guide explains how to create files of a certain size in 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, 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 man pages.
$ man truncate
2. Create files of a certain 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 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 this command.
$ man head
4. Ceate 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 certain size as well.
To create a file with size 5MB, run:
$ dd if=/dev/urandom of=ostechnix.txt bs=5MB count=1
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 man pages.
$ man dd
- How To Find The Size Of A Directory In Linux
- The mktemp Command Tutorial With Examples For Beginners
And, that's all. You know now how to create a file with certain size. As you can see in the above examples, creating files of certain size is no big deal. Hope this helps.