Linux command
mktemp 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Example
mktemp
Example
mktemp -p [/path/to/directory]
Example
mktemp [/tmp/example.XXXXXXXX]
Example
mktemp --suffix [.txt]
Example
mktemp -d
Dry run
mktemp -u
说明
mktemp creates temporary files or directories safely and prints their paths. The template must include at least 3 consecutive X characters in its final component, which are replaced with random alphanumeric characters to ensure uniqueness. When no template is provided, mktemp defaults to tmp.XXXXXXXXXX in the system temp directory. Files are created with u+rw permissions and directories with u+rwx, both modified by umask. The safe creation prevents race conditions where another process might create a file between checking for existence and creating. This is critical for secure script writing.
参数
- -d, --directory
- Create a directory instead of a file.
- -u, --dry-run
- Print name without creating anything (unsafe; see CAVEATS).
- -q, --quiet
- Suppress error messages on creation failure.
- -p _dir_, --tmpdir=_dir_
- Create temporary file relative to the specified directory; defaults to $TMPDIR or /tmp.
- -t
- Interpret template as a filename relative to the temp directory (deprecated).
- --suffix _suff_
- Append suffix to template; suffix must not contain slashes.
FAQ
What is the mktemp command used for?
mktemp creates temporary files or directories safely and prints their paths. The template must include at least 3 consecutive X characters in its final component, which are replaced with random alphanumeric characters to ensure uniqueness. When no template is provided, mktemp defaults to tmp.XXXXXXXXXX in the system temp directory. Files are created with u+rw permissions and directories with u+rwx, both modified by umask. The safe creation prevents race conditions where another process might create a file between checking for existence and creating. This is critical for secure script writing.
How do I run a basic mktemp example?
Run `mktemp` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -d, --directory do in mktemp?
Create a directory instead of a file.