Create a Large File from the Command Line or Disk Utility for Testing Purposes
Large empty files are often used for testing purposes during disk access tests, development, QA, zeroing out data, and scripting. Though it’s certainly not applicable to most users, it’s easy enough to do that anyone can try it out even if you don’t have a specific need.
We’ll cover three ways to quickly generate files of virtually any size, two will use the command line; one being operating system agnostic and the other MacOS and Mac OS X specific, and another more user friendly approach that utilizes the Disk Utility app native to Mac OS X.
This is obviously aimed at a bit more advanced users with some fluency in the command line. To follow along, open the Terminal app to get started.
Create a Large File from the Command Line
The simplest way to instantly generate a large empty file is to use the ‘mkfile’ command, which can immediately create a file of any size, whether fairly small in bytes or huge in gigabytes. Syntax for mkfile is as follows:
mkfile -n size[b|k|m|g] filename
For example, to create a 1GB file called “LargeTestFile” on the desktop, the command would be:
mkfile -n 1g ~/Desktop/LargeTestFile
The file is created instantly and takes up the full size. Big files created from mkfile are full of zeroes.
You can confirm the generated file size with the finder Get Info command, or by using ls:
ls -lh ~/Desktop/LargeTestFile
The only downside to the mkfile command is that it appears limited to Mac OS X, thus if you’re looking for a cross-platform compatible solution that will work across other unix and linux variations you will want to use “dd” instead.
The dd command is a little less obvious to use than mkfile, but it’s stil fairly straight forward, you need to specify a file name, a block size, and a block count:
dd if=/dev/zero of=FileName bs=1024 count=1000
Another approach is to use the seek flag with some simple multiplication of a megabyte block size (1024), thus the following command would create a file that is 100MB in size (1024 x 100):
dd if=/dev/zero of=LargeTestFile.img bs=1024 count=0 seek=$[1024*100]
The latter multiplication method from CyberCit can be a bit easier if you’re not great at estimating large byte sizes.
Creating a Huge File with Disk Utility
Though most users looking to create large empty files are probably going to prefer the command line, you can also use Disk Utility.
- Launch Disk Utility and choose “New Image”
- Name the file as appropriate, then pull down the “Size” sub menu and select a file size appropriate for your needs
- Ignore all other settings and choose “Create”
DiskUtility will craft a disk image of the size specified, which works great for testing. Locate the newly crafted DMG in the Finder and you’ll see it takes up the full size that was specified, in this case the size of a 2.6GB DVD:
Unlike dd or mkfile, the disk image will actually be writable by default unless otherwise chosen, which may or may not be useful for this development purposes.
Whichever method you use, you’ll probably want to delete the large test file(s) afterwards, or else your hard disk can quickly be eaten up by the otherwise useless test files of enormous size. If you created the test files in an obscure folder and you can no longer locate them on your own, don’t forget that you can perform file size specific searches with Spotlight in the OS X Finder to quickly track down any large item in the file system.
Hi,
You have to note that mkfile files does not consume free space. As manual says:
“The size is noted, but disk blocks aren’t allocated until data is written to them.”
I was surprised when I created a 10gb file on a 8gb partition.
Found that it’s better to populate file with random data:
head -c $[1024*1024*1024] < /dev/urandom > myfile111
This is brilliant! Thank you! Being able to create a 3GB file in less than a minute is so incredibly helpful for testing minimal disk space.