mktemp命令用于建立暂存文件或者文件夹,帮助文档如下:
Usage: mktemp [OPTION]... [TEMPLATE]
Create a temporary file or directory, safely, and print its name.
TEMPLATE 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 restrictions.
-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
Linux系统有特殊的目录,专供临时文件使用。Linux使用/tmp目录来存放不需要永久保留的文件。mktemp命令专门用来创建临时文件,并且其创建的临时文件是唯一的。shell会根据mktemp命令创建临时文件,但不会使用默认的umask值(管理权限的)。它会将文件的读写权限分配给文件属主,一旦创建了文件,在shell脚本中就拥有了完整的读写权限,其他人不可访问(除了root)。
用法如下:
linux-UMLhEm:/home # mktemp abc.XXX abc.cJD
#默认生成在系统/tmp目录下,生成的文件模板为tmp.XXXXXXXXXX linux-UMLhEm:/home # mktemp /tmp/tmp.Yr5NTRV3sj linux-UMLhEm:/home # mktemp -t /tmp/tmp.LMEdNCrPJN
#自定义文件模板,并且生成在系统目录下 linux-UMLhEm:/home # mktemp -t abc.XXXXXX /tmp/abc.wn27Pg
#生成临时目录 linux-UMLhEm:/home # mktemp -d /tmp/tmp.UuZ8TIdxwa linux-UMLhEm:/home # file /tmp/tmp.UuZ8TIdxwa/ /tmp/tmp.UuZ8TIdxwa/: directory
#文件模板XXX不能少于3个 linux-UMLhEm:/home # mktemp -d --tmpdir=/home abc.XX mktemp: too few X's in template ‘abc.XX’
#在指定目录下生成临时文件 linux-UMLhEm:/home # mktemp -d --tmpdir=/home abc.XXX /home/abc.2i3 linux-UMLhEm:/home #