As the title suggests — can I be reasonably sure that mktemp
will exist on any unix-y operating system I'm likely to encounter?

- 148,955
- 89
- 346
- 502
-
See also https://unix.stackexchange.com/a/84980/37512. – Timo Tijhof Mar 07 '19 at 12:41
4 Answers
POSIX does not seem to specify mktemp(1).
It looks like most modern systems have it, but the available functionality and the semantics of the options vary between implementations (so particular invocations may not be portable):
- mktemp(1) from OpenBSD — mktemp(1) originated in OpenBSD 2.1
- mktemp(1) from FreeBSD
- mktemp(1) from Mac OS X — almost always the same as from FreeBSD
- mktemp(1) from Todd C. Miller of sudo fame
- mktemp(1) from Solaris
- mktemp(1) from GNU coreutils
- mktemp(1) from HP/UX — this one seems particularly divergent from most of the others listed here
So if you want a portable solution you may need to stick to functionality and options that mean the same thing on all of your platforms of interest.

- 18,476
- 5
- 50
- 64

- 214,407
- 26
- 209
- 186
-
1It use to be reasonably portable and defined by POSIX but it seems that some weaknesses in it were addressed by forming a new function 'mkstemp()'. "4.3BSD, POSIX.1-2001. POSIX.1-2008 removes the specification of mktemp()." http://man7.org/linux/man-pages/man3/mktemp.3.html The newer replacment being http://man7.org/linux/man-pages/man3/mkstemp.3.html – TafT Apr 28 '15 at 07:55
-
3The `mktemp` @TafT is addressing is the C library function, whereas the `mktemp` OP asked about is the command line program. – onlynone Aug 15 '17 at 16:05
-
@onlynone is one of things not often a very thin veneer around the other? – TafT Aug 16 '17 at 16:40
-
4@TafT kind of. But it's not quite that thin. `mktemp.c` in gnu coreutils is 350 lines: http://lingrok.org/xref/coreutils/src/mktemp.c . And actually it doesn't look like it ever calls `mktemp(3)` instead it uses some gnulib function (not glibc) that does it's own logic and the only system call it makes is to `open`. – onlynone Aug 17 '17 at 15:47
-
@TafT No - no proper implementation of `mktemp` the command line tool will use `mktemp` the deprecated C function. – mtraceur May 16 '20 at 05:13
-
HP/UX had `mktemp(1)` in 10.20 already (1996) and the revision control header in the man page had `$Header: mktemp.1,v 72.3 93/01/14 11:27:33 ssa Exp $` suggesting it had been there for much longer. So HP/UX had a mktemp command long before OpenBSD (1997). – Stephane Chazelas Oct 18 '20 at 06:29
-
Todd Miller did write OpenBSD's `mktemp` in the first place and later made it a portable utility, the one used on Linux distributions until GNU wrote their own compatible one. – Stephane Chazelas Oct 18 '20 at 06:38
-
Actually, [HP/UX 7.0 (1989) already had a mktemp utility](https://archive.org/stream/bitsavers_hp9000hpux0ReferenceVol1Section1Sep89_33991967/09000-90013_HP-UX_7.0_Reference_Vol_1_Section_1_Sep89_djvu.txt) and it looks like 6.x didn't. Ultrix 4.0 (1990) also had a mktemp with the same API. – Stephane Chazelas Oct 18 '20 at 08:05
A mktemp
function (AKA mktemp(3)
) first appeared in Unix V7 so it's likely to be everywhere. However, a mktemp
command (aka mktemp(1)
) first appeared, I believe, on OpenBSD 2.1, so if you have to deal with truly antediluvian Unix systems you might have to worry -- unless you can distribute the very portable mktemp.org version (to fix the potential lack of this utility on some customer's antediluvian system). How likely you are to encounter antediluvian system is nigh impossible for us to guess, of course -- e.g., in HP-UX
, mktemp(1)
has been around for at least 8 years (even most enterprises probably have updated their Unix OS's within that time frame), in Xenix I believe it appeared in 3.0 (in 1992), etc, etc.

- 854,459
- 170
- 1,222
- 1,395
-
So the short summary is: yes, you can reasonably rely on mktemp being around – mpez0 May 08 '10 at 02:39
FYI, mktemp appears to NOT be included with Solaris 9 (released 2002/2003) - just ran across this today:
$ uname -a SunOS dcmnapp02 5.9 Generic_122300-47 sun4u sparc SUNW,Sun-Fire-V440 $ mktemp bash: mktemp: command not found $ man mktemp bash-2.05$ man mktemp Reformatting page. Please Wait... done Standard C Library Functions mktemp(3C) NAME mktemp - make a unique file name SYNOPSIS #include char *mktemp(char *template);

- 641
- 7
- 18
-
2Yes, Solaris 10 was the first Solaris release to include the mktemp(1) command in the OS. – alanc Aug 18 '12 at 06:55
On Solaris 9 it's in package SMCmktemp, see http://sunfreeware.com/indexsparc9.html:
uname -s
SunOS
uname -r
5.9
/usr/sbin/pkgchk -l -p /usr/local/bin/mktemp
Pathname: /usr/local/bin/mktemp
Type: regular file
Expected mode: 0555
Expected owner: bin
Expected group: bin
Expected file size (bytes): 8884
Expected sum(1) of contents: 6493
Expected last modification: Nov 05 08:48:17 2002
Referenced by the following packages:
SMCmktemp
Current status: installed

- 61
- 2
-
2
-
Yes, the service has moved to https://unixpackages.com and has become a paid service. – thecarpy Apr 14 '23 at 06:51