国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > mktemp 以获得更好的体验。

# Bash 的 mktemp 命令

mktemp [OPTION]... [TEMPLATE]

!subtitle:功能

安全的创建临时文件或目录,并打印名称。

!subtitle:类型

可执行文件(/usr/bin/mknod),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • -d, --directory - 创建临时目录,而不是临时文件

    • -u, --dry-run - 只打印临时文件名,而不创建文件

    • -q, --quiet - 不打印失败信息

    • --suffix=SUFF - 在文件名 TEMPLATE 后面添加后缀 SUFF

    • -p DIR, --tmpdir[=DIR] - 相对于 DIR 目录创建临时文件

    • -t - 相对于环境变量 TMPDIR 表示的目录创建临时文件;已废弃

    • --help - 显示帮助

    • --version - 显示版本

  • TEMPLATE - 临时文件名模板,末尾至少 3 个 X 表示临时名称;例如 my_temp.XXX

    • 不指定 TEMPLATE 参数时,默认格式为 tmp.XXXXXXXXXX,并默认设置 --tmpdir=/tmp

    • 指定 TEMPLATE 参数时,不自动设置 --tmpdir 选项

# 示例

$ mktemp                        # 创建临时文件
/tmp/tmp.MWGmRyLisU
$ mktemp my_temp.XXX            # 在当前目录创建临时文件
my_temp.EQ4
$ mktemp -p /tmp my_temp.XXX    # 在 /tmp 目录创建自定义格式的临时文件
/tmp/my_temp.PZj

# 相关命令

命令 说明
touch 创建普通文件或刷新访问时间
mkdir 创建目录
mkfifo 创建命名管道
mknod 创建设备文件
link 创建硬链接
ln 创建硬链接或符号链接

# 推荐阅读

# 手册

MKTEMP(1)                        User Commands                       MKTEMP(1)

NAME
       mktemp - create a temporary file or directory

SYNOPSIS
       mktemp [OPTION]... [TEMPLATE]

DESCRIPTION
       Create a temporary file or directory, safely, and print its name.  TEM‐
       PLATE  must  contain at least 3 consecutive 'X's in last component.  If
       TEMPLATE is not specified, use tmp.XXXXXXXXXX, and --tmpdir is implied.
       Files are created u+rw, and directories  u+rwx,  minus  umask  restric‐
       tions.

       -d, --directory
              create a directory, not a file

       -u, --dry-run
              do not create anything; merely print a name (unsafe)

       -q, --quiet
              suppress diagnostics about file/dir-creation failure

       --suffix=SUFF
              append  SUFF  to  TEMPLATE; SUFF must not contain a slash.  This
              option is implied if TEMPLATE does not end in X

       -p DIR, --tmpdir[=DIR]
              interpret TEMPLATE relative to DIR; if DIR is not specified, use
              $TMPDIR if set, else /tmp.  With this option, TEMPLATE must  not
              be  an  absolute  name;  unlike  with  -t,  TEMPLATE may contain
              slashes, but mktemp creates only the final component

       -t     interpret TEMPLATE as a single file name component, relative  to
              a  directory:  $TMPDIR, if set; else the directory specified via
              -p; else /tmp [deprecated]

       --help display this help and exit

       --version
              output version information and exit

AUTHOR
       Written by Jim Meyering and Eric Blake.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2023 Free Software Foundation, Inc.   License  GPLv3+:  GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This  is  free  software:  you  are free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       mkstemp(3), mkdtemp(3), mktemp(3)

       Full documentation <https://www.gnu.org/software/coreutils/mktemp>
       or available locally via: info '(coreutils) mktemp invocation'

GNU coreutils 9.4                 April 2024                         MKTEMP(1)
本文 更新于: 2025-11-27 09:38:15 创建于: 2025-11-27 09:38:15