/tmp on OS X

/tmp on OS X

Under Linux and most other Un*x systems, you can rely on the directory /tmp being the system scratch space. As such, you can pretty much use /tmp without fear.

On OS X, each user has its own scratch space. Use the NSTemporaryDirectory() function to retrieve the directory name.

Here is a small bit of Objective C example code.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// print the current user's scratch directory.
NSLog(NSTemporaryDirectory());

[pool drain];
return 0;
}

Update 2009-07-27:
On the command line, the TMPDIR environment variable holds the same value that NSTemporaryDirectory() returns.

  1. Harley Pebley Says:

    That’s cool that each user has their own /tmp space. Hope as sys admin you can specify where it is (like /home). Once found out the hard way /tmp should not be a mount point on / (the default, at least on the dist I was on). Since then I always give it its own partition.

  2. James Reuben Knowles Says:

    “Once found out the hard way /tmp should not be a mount point on /”

    You, too, eh? War story time. :)

    I haven’t dug into system internals enough to know if there’s a simple way to tell OS X to use a separate partition for temp directories.

    Interestingly, /etc /tmp and /var are all symlinks into /private, which appears to be marked with extended attributes. I don’t know enough about BSD-specific intricacies to know what’s going on, but the per-user configurability make me suspect that union filesystem features may be used — but that’s pure speculation.

Leave a Reply