{
    "mode": "info",
    "parameter": "mktemp",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/mktemp/json",
    "generated": "2026-09-23T11:49:34Z",
    "sections": {
        "File: coreutils.info,  Node: mktemp invocation,  Next: realpath invocation,  Prev: pathchk invocation,  Up: File name manipulation": {
            "content": "",
            "subsections": [
                {
                    "name": "18.4 'mktemp': Create temporary file or directory",
                    "content": "'mktemp' manages the creation of temporary files and directories.\nSynopsis:\n\nmktemp [OPTION]... [TEMPLATE]\n\nSafely create a temporary file or directory based on TEMPLATE, and\nprint its name.  If given, TEMPLATE must include at least three\nconsecutive 'X's in the last component.  If omitted, the template\n'tmp.XXXXXXXXXX' is used, and option '--tmpdir' is implied.  The final\nrun of 'X's in the TEMPLATE will be replaced by alpha-numeric\ncharacters; thus, on a case-sensitive file system, and with a TEMPLATE\nincluding a run of N instances of 'X', there are '62N' potential file\nnames.\n\nOlder scripts used to create temporary files by simply joining the\nname of the program with the process id ('$$') as a suffix.  However,\nthat naming scheme is easily predictable, and suffers from a race\ncondition where the attacker can create an appropriately named symbolic\nlink, such that when the script then opens a handle to what it thought\nwas an unused file, it is instead modifying an existing file.  Using the\nsame scheme to create a directory is slightly safer, since the 'mkdir'\nwill fail if the target already exists, but it is still inferior because\nit allows for denial of service attacks.  Therefore, modern scripts\nshould use the 'mktemp' command to guarantee that the generated name\nwill be unpredictable, and that knowledge of the temporary file name\nimplies that the file was created by the current script and cannot be\nmodified by other users.\n\nWhen creating a file, the resulting file has read and write\npermissions for the current user, but no permissions for the group or\nothers; these permissions are reduced if the current umask is more\nrestrictive.\n\nHere are some examples (although note that if you repeat them, you\nwill most likely get different file names):\n\n* Create a temporary file in the current directory.\n$ mktemp file.XXXX\nfile.H47c\n\n* Create a temporary file with a known suffix.\n$ mktemp --suffix=.txt file-XXXX\nfile-H08W.txt\n$ mktemp file-XXXX-XXXX.txt\nfile-XXXX-eI9L.txt\n\n* Create a secure fifo relative to the user's choice of 'TMPDIR', but\nfalling back to the current directory rather than '/tmp'.  Note\nthat 'mktemp' does not create fifos, but can create a secure\ndirectory in which the fifo can live.  Exit the shell if the\ndirectory or fifo could not be created.\n$ dir=$(mktemp -p \"${TMPDIR:-.}\" -d dir-XXXX) || exit 1\n$ fifo=$dir/fifo\n$ mkfifo \"$fifo\" || { rmdir \"$dir\"; exit 1; }\n\n* Create and use a temporary file if possible, but ignore failure.\nThe file will reside in the directory named by 'TMPDIR', if\nspecified, or else in '/tmp'.\n$ file=$(mktemp -q) && {\n>   # Safe to use $file only within this block.  Use quotes,\n>   # since $TMPDIR, and thus $file, may contain whitespace.\n>   echo ... > \"$file\"\n>   rm \"$file\"\n> }\n\n* Act as a semi-random character generator (it is not fully random,\nsince it is impacted by the contents of the current directory).  To\navoid security holes, do not use the resulting names to create a\nfile.\n$ mktemp -u XXX\nGb9\n$ mktemp -u XXX\nnzC\n\nThe program accepts the following options.  Also see *note Common\noptions::.\n\n'-d'\n'--directory'\nCreate a directory rather than a file.  The directory will have\nread, write, and search permissions for the current user, but no\npermissions for the group or others; these permissions are reduced\nif the current umask is more restrictive.\n\n'-q'\n'--quiet'\nSuppress diagnostics about failure to create a file or directory.\nThe exit status will still reflect whether a file was created.\n\n'-u'\n'--dry-run'\nGenerate a temporary name that does not name an existing file,\nwithout changing the file system contents.  Using the output of\nthis command to create a new file is inherently unsafe, as there is\na window of time between generating the name and using it where\nanother process can create an object by the same name.\n\n'-p DIR'\n'--tmpdir[=DIR]'\nTreat TEMPLATE relative to the directory DIR.  If DIR is not\nspecified (only possible with the long option '--tmpdir') or is the\nempty string, use the value of 'TMPDIR' if available, otherwise use\n'/tmp'.  If this is specified, TEMPLATE must not be absolute.\nHowever, TEMPLATE can still contain slashes, although intermediate\ndirectories must already exist.\n\n'--suffix=SUFFIX'\nAppend SUFFIX to the TEMPLATE.  SUFFIX must not contain slash.  If\n'--suffix' is specified, TEMPLATE must end in 'X'; if it is not\nspecified, then an appropriate '--suffix' is inferred by finding\nthe last 'X' in TEMPLATE.  This option exists for use with the\ndefault TEMPLATE and for the creation of a SUFFIX that starts with\n'X'.\n\n'-t'\nTreat TEMPLATE as a single file relative to the value of 'TMPDIR'\nif available, or to the directory specified by '-p', otherwise to\n'/tmp'.  TEMPLATE must not contain slashes.  This option is\ndeprecated; the use of '-p' without '-t' offers better defaults (by\nfavoring the command line over 'TMPDIR') and more flexibility (by\nallowing intermediate directories).\n\nExit status:\n\n0 if the file was created,\n1 otherwise.\n"
                }
            ]
        }
    },
    "flags": [],
    "examples": [],
    "see_also": []
}