7 Jun 2025

Using `mktemp` to Safely Create Temporary Files and Directories

Using `mktemp` to Safely Create Temporary Files and Directories

This post reviews the `mktemp` command, how it works, and why it should be your default choice for temporary file handling.

Whether you’re writing a shell script or working in the terminal, creating temporary files can be risky if done incorrectly. If multiple processes or users are working in the same directory, name collisions or security risks can arise. That’s where the mktemp command comes in — it safely creates unique, secure temporary files or directories.

This post reviews the mktemp command, how it works, and why it should be your default choice for temporary file handling.



What mktemp Does

The mktemp command generates a unique temporary filename or directory. It avoids race conditions by ensuring the file or directory does not already exist, and it uses secure permissions by default.

It’s especially useful in:

  • Shell scripts
  • One-liners involving temp data
  • Situations where file collisions would cause bugs or expose sensitive data


Basic Usage: Create a Temporary File

The simplest way to use mktemp is:

mktemp

This creates a file like:

/tmp/tmp.ABcD1234

By default, it’s created in /tmp with 0600 permissions (read/write by owner only).


Customize the Filename Template

You can control the format using a custom template:

mktemp /tmp/mytemp.XXXXXX

Note: The Xs are required — they get replaced with random characters. If you don’t include at least six Xs, mktemp will throw an error.

Example result:

/tmp/mytemp.n9gLXa


Create a Temporary Directory

To create a temp directory instead of a file:

mktemp -d

Or with a template:

mktemp -d /tmp/mydir.XXXXXX

This will return a unique directory path and create it immediately.


Assign the Path to a Variable

When scripting, you’ll often store the result of mktemp:

TMPFILE=$(mktemp)
echo "Temporary file is: $TMPFILE"

Or for directories:

TMPDIR=$(mktemp -d)
echo "Temporary dir is: $TMPDIR"


Clean Up

Remember to remove temp files and dirs when you’re done:

rm "$TMPFILE"
rm -r "$TMPDIR"

You can also use trap in a script to auto-clean on exit:

TMPFILE=$(mktemp)
trap "rm -f $TMPFILE" EXIT


Use Case: Writing to a Temp File Then Moving It

One common pattern is writing to a temporary file, then moving it into place:

TMP=$(mktemp)
/usr/bin/generate_config > "$TMP"
mv "$TMP" /etc/myapp/config.conf

This helps avoid leaving a broken or partial file behind if the script fails mid-write.


Conclusion

mktemp is a small but powerful command that solves a very real problem: safely and predictably handling temporary files. By always using it instead of hardcoded filenames like /tmp/foo, you avoid name collisions and boost security — especially in multi-user or automated environments.

Next time you’re building a shell script, don’t reinvent the wheel — use mktemp for all your temporary file needs.


📝 For more information about mktemp, please review the man page.