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 #