mktemp
Files and directories
Usage
mktemp [-d]Creates a temp file or directory with a name nobody can guess and prints its path; it never cleans up after itself, so pair it with a trap on exit.
Common flags
| Flag | Meaning |
|---|---|
| -d | Create a directory instead of a file |
| -t | Build the name from a prefix inside the temp directory |
| -p | Choose the parent directory |
| -u | Only print a name, create nothing. Unsafe, avoid it |
| -q | Say nothing when it fails |
Examples
tmp=$(mktemp)A private temp file, with its path in $tmp.
dir=$(mktemp -d)A temp directory to work in.
mktemp /tmp/build.XXXXXXThe trailing Xs become random characters.
Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does mktemp do?
Creates a temp file or directory with a name nobody can guess and prints its path; it never cleans up after itself, so pair it with a trap on exit.
Q. How do I type it?
mktemp [-d] — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
5 are listed here; the full set is in man mktemp. This command sits under Files and directories.
Related commands
man page: man mktemp