Determine current user temp directory on macOS

901 Views Asked by At

How to print specific user temp directory in macOS? It should be able to determine the temp dir depending on which user is currently logged in. I see that my current user temp dir is set to /Users/myuser/private/tmp but i have no generic way to get hold of this

Googled a bit and it recommeds using $TMPDIR but looks like my user temp dir is somewhat set to /Users/myuser/private/tmp

I should be able to determine the exact tmp directory whether the current user is root or other user

1

There are 1 best solutions below

1
NuclearPeon On

Related SO question and answer, cross-platform: Cross-platform way of getting temp directory in Python It's pretty generic.

In Qt5:

#include <QCoreApplication>
#include <QTemporaryDir>
#include <QDebug>

int main(int argc, char *argv[])
{
    QTemporaryDir dir;
    if (dir.isValid()) {
        // dir.path() returns the unique directory path
        qDebug() << "Path name: " << dir.path() ;
    }
}

Also pretty generic.

In bash (terminal or script): echo $TMPDIR

The /var/folders/ is user-specific because if you run ls -l /var/folders/.../ (right before the -Tmp- directory, you will see that the folder is owned by the logged in user. If another user is logged in, their folder is protected by their permissions/ownership and they won't be able to read/modify another users' data. I suggest looking into unix/linux/mac file and folder permissions if you want to know more.

new-host-2:Applications ThisIsMe$ ls -l /var/folders/cp/cpI9fK8qG3y4x4sedjGIOE+++TI/ 
total 0
drwx------  16 ThisIsMe  staff  544 Jan  6 22:12 -Caches-
drwx------  11 ThisIsMe  staff  374 Jan  6 22:56 -Tmp-

http://www.filepermissions.com/articles/what-are-file-permissions-in-linux-and-mac https://www.linux.com/tutorials/understanding-linux-file-permissions/