mktemp on macOS random file generation fails: file exists

1.2k Views Asked by At

When I create a random file on macOS with mktemp I get:

mktemp /tmp/test.XXXXXXX.json
mktemp: mkstemp failed on /tmp/test.XXXXXXX.json: File exists

It does not replace XXXXXXX with random string. Works fine on Linux but not on macOS.

2

There are 2 best solutions below

2
On

This is the documented behaviour, from man mktemp:

The template may be any file name with some number of `Xs' appended to it, for example /tmp/temp.XXXXXX

0
On

Turns out, for whatever reason, macOS version of mktemp supports only trailing XXXXXX for random string, in other words mktemp /tmp/test.XXXX will work but not /tmp/test.XXXX.json