简体   繁体   English

确定 macOS 上的当前用户临时目录

[英]Determine current user temp directory on macOS

How to print specific user temp directory in macOS?如何在 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它应该能够根据当前登录的用户确定临时目录。我看到我当前的用户临时目录设置为 /Users/myuser/private/tmp 但我没有通用的方法来掌握这个

Googled a bit and it recommeds using $TMPDIR but looks like my user temp dir is somewhat set to /Users/myuser/private/tmp谷歌了一下,它推荐使用 $TMPDIR 但看起来我的用户临时目录有点设置为 /Users/myuser/private/tmp

I should be able to determine the exact tmp directory whether the current user is root or other user我应该能够确定当前用户是 root 用户还是其他用户的确切 tmp 目录

Related SO question and answer, cross-platform: Cross-platform way of getting temp directory in Python It's pretty generic.相关的 SO 问答,跨平台: 在 Python 中获取临时目录的跨平台方式它非常通用。

In Qt5:在 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在 bash(终端或脚本)中: 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. /var/folders/特定于用户的,因为如果您运行ls -l /var/folders/.../ /var/folders/ ls -l /var/folders/.../ (就在-Tmp-目录之前,您将看到该文件夹​​归登录用户所有。如果另一个用户已登录,他们的文件夹受其权限/所有权保护,他们将无法读取/修改其他用户的数据。如果您想了解更多信息,我建议查看 unix/linux/mac 文件和文件夹权限.

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/ http://www.filepermissions.com/articles/what-are-file-permissions-in-linux-and-mac https://www.linux.com/tutorials/understanding-linux-file-permissions/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM