Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Generate fake files of any size in macOS and Linux

Oftentimes when working with applications, for the sake of testing things out we need placeholder files. For instance, if we have a file upload functionality, we may want to test it for a certain file type of a certain size.

So, if we want to test if the file upload functionality restricts uploading PDF files to not more than 10 MBs, we need a PDF file with a size of more than 10 MBs for it to fail.

It could be possible, we may not have the exact file of this size and that’s where these little utilities in macOS and Linux can come in handy.

The mkfile utility in macOS

The mkfile utility in macOS lets you create fake files of any size you may need.

So, for instance, if we want a PDF file of size 10 MB, here’s how you can generate it.

$ mkfile 10m fakefile.pdf

This will generate the desired PDF file at the specified location. You might not be able to open it since it’s just a fake file built using random binaries but it will get your job done.

Here are few more examples.

$ mkfile 5m fakeimage.png
$ mkfile 15m fakevideo.mp4

The truncate utility in Linux

If you want to achieve the same in Linux, it has got the truncate utility which will essentially let you do that.

Here’s how you can do it.

$ truncate -s 10m fakefile.pdf

Here, the -s option lets you specify the size of the file.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?