Thursday, July 20, 2017

How To Create Files Of A Certain Size In Linux

How To Create Files Of A Certain Size In Linux


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.

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.

Method 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

Method 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 – 5*1024*1024=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

Method 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

Method 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 certain size as well.
To create a file with size 5MB, run:
dd if=/dev/urandom of=ostechnix.txt bs=5MB count=1
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


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.

No comments: