🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

mktemp Command

Beginner File Management man(1)

Create temporary files and directories securely

📅 Updated: Mar 16, 2026
SYNTAX
mktemp [OPTIONS] [TEMPLATE]

What Does mktemp Do?

The mktemp command creates temporary files or directories with unique, unpredictable names. It is the secure way to handle temporary files in shell scripts, eliminating race conditions and symlink attacks that plague naive approaches like using fixed filenames in /tmp.

mktemp generates a random suffix (replacing X characters in the template) and creates the file with restrictive permissions (0600 for files, 0700 for directories), ensuring only the creating user can access the temporary resource. It outputs the path to the created file/directory, which the script captures for use.

Using mktemp is a security best practice. Without it, scripts that use predictable temp filenames (like /tmp/mydata.$$) are vulnerable to symlink attacks where an attacker pre-creates a symlink at the expected path, potentially causing the script to overwrite critical files or leak sensitive data.

Options & Flags

OptionDescriptionExample
(no options) Create a temporary file in /tmp mktemp
-d Create a temporary directory instead of a file mktemp -d
-p DIR Create temp file in specified directory mktemp -p /var/tmp
-t NAME Use NAME as template prefix mktemp -t myapp.XXXXXX
--suffix=SUFF Append suffix to template mktemp --suffix=.json
-u Unsafe mode - print name without creating (not recommended) mktemp -u
TEMPLATE Custom template with X placeholders mktemp /tmp/backup.XXXXXXXX

Practical Examples

#1 Create temporary file

Create a temp file, write to it, read it, and clean up. The file has 0600 permissions.
$ tmpfile=$(mktemp) && echo "Data" > "$tmpfile" && cat "$tmpfile" && rm "$tmpfile"
Output: Data

#2 Create temporary directory

Create a temp directory with 0700 permissions. Use for multiple temp files or complex operations.
$ tmpdir=$(mktemp -d) && echo "Dir: $tmpdir" && ls -la "$tmpdir" && rm -rf "$tmpdir"

#3 Script with automatic cleanup

Use trap EXIT to automatically clean up temp files when the script exits (normally or on error).
$ tmpfile=$(mktemp) && trap "rm -f $tmpfile" EXIT && curl -s https://example.com > "$tmpfile" && wc -l "$tmpfile"

#4 Custom template with suffix

Create a temp file with .csv extension and custom prefix. Useful when tools require specific file extensions.
$ mktemp --suffix=.csv /tmp/report.XXXXXX
Output: /tmp/report.a3bK9x.csv

#5 Temp file in specific directory

Create temp file in /var/tmp (survives reboots on some systems) instead of /tmp.
$ mktemp -p /var/tmp backup.XXXXXXXX
Output: /var/tmp/backup.4f8Gk2x1

#6 Secure pipeline with temp files

Sort a file in-place safely. Write to temp file first, then replace original. Prevents data loss if sort fails.
$ tmp=$(mktemp) && sort input.txt > "$tmp" && mv "$tmp" input.txt

Tips & Best Practices

Always use trap for cleanup: In scripts, use: trap "rm -f $tmpfile" EXIT — this ensures temp files are removed even if the script fails or is interrupted with Ctrl+C.
Never use predictable temp names: Never use /tmp/myapp.$$ or /tmp/data.tmp. These are predictable and vulnerable to symlink attacks. Always use mktemp.
XXXXXX minimum: Templates must have at least 3 consecutive X characters. More Xs mean more randomness. The default template uses 10 random characters.
Use $TMPDIR: mktemp respects the $TMPDIR environment variable. Set it to use a different base directory for all temp files.

Frequently Asked Questions

How do I create a temporary file in a shell script?
Use: tmpfile=$(mktemp) — this creates a secure temp file and stores the path. Always clean up with trap "rm -f $tmpfile" EXIT at the start of your script.
Where does mktemp create files?
By default in /tmp (or $TMPDIR if set). Use -p DIR to specify a different directory. Use -d to create a directory instead.
How many X characters should I use in the template?
At least 3 (required), but 6-10 is recommended for sufficient randomness. The default template uses enough X characters for security.
Why not just use /tmp/myfile.tmp?
Fixed names are a security risk (symlink attacks). If an attacker creates a symlink at /tmp/myfile.tmp pointing to /etc/passwd, your script could overwrite it. mktemp uses unpredictable names.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →