Files
tempfile
Create a temporary file safely.
Additional Notes
tempfile creates a temporary file with a unique name in a secure way. The generated filename is printed to standard output so it can be captured by a script. It avoids race conditions and symlink attacks that can occur when using naive methods like mktemp with user-generated names.
The file is created in $TMPDIR if set, or in /tmp by default. On Debian-based systems it is provided by the debianutils package. Many modern scripts prefer mktemp which is more portable across Linux distributions.
Syntax
tempfile [options]
Parameters
No positional parameters. The temporary file path is returned on stdout.
Common Options
-d,--directory: Create a temporary directory instead of a file.-p string,--prefix string: Usestringas the prefix for the filename (defaults totmp).-s string,--suffix string: Usestringas the suffix for the filename.-m mode,--mode mode: Set the file permission mode (e.g.0600).--help: Display help and exit.
Examples
tempfile
Create a temporary file. Output: /tmp/tmp.XXXXXX
TMPFILE=$(tempfile)
echo "Log data" > "$TMPFILE"
rm "$TMPFILE"
Create a temp file, write to it, then remove it.
tempfile -p backup- -s .txt
Create a temp file with a custom prefix and suffix.
Practical Notes
tempfileis less portable thanmktemp. Usemktempfor scripts that should work across BSD, macOS, and other Unix-like systems.- Always clean up temporary files in scripts, ideally with a
traphandler. - Use
trap 'rm -f "$TMPFILE"' EXITto ensure cleanup on script exit.