tempfile
Create temporary files or directories
SYNOPSIS
tempfile [-d] [-q] [-p prefix] [-s suffix] [-m mode] [-t dir] [-k] [-n] [template]
PARAMETERS
-d
Create a temporary directory instead of a file.
-q
Suppress error messages during operation.
-p prefix
Specify a prefix string for the temporary filename. Defaults to 'tmp.'.
-s suffix
Specify a suffix string for the temporary filename.
-m mode
Set the permissions (octal mode) for the created file or directory.
-t dir
Specify the directory where the temporary file or directory will be created. Overrides TMPDIR.
-k
Keep the temporary file or directory after the program exits, preventing automatic deletion.
-n
Do not actually create the file or directory; only print the generated name.
template
A template for the filename, usually containing 'X' characters (e.g., 'myprog.XXXXXX') which will be replaced by unique characters. If not provided, a default template like 'tmp.XXXXXX' is used.
DESCRIPTION
The tempfile command is a utility designed to securely create unique temporary file or directory names. It helps prevent race conditions and avoids conflicts with existing files, making it ideal for shell scripts requiring temporary storage. Users can specify a prefix, suffix, and permissions for the created item, as well as an explicit temporary directory using the -t option. By default, tempfile tries to use directories specified by the TMPDIR environment variable, falling back to common temporary paths like /tmp. It prints the generated unique path to standard output, which can then be used by other commands or scripts. While functional, it is now largely considered deprecated in favor of the more modern and flexible mktemp utility.
CAVEATS
The tempfile command is considered deprecated. Users and scripts are strongly encouraged to use mktemp(1) instead, which provides a more robust, flexible, and actively maintained solution for creating secure temporary files and directories. Relying on tempfile in new development is not recommended.
Using the -k option requires careful manual cleanup to prevent accumulation of temporary files.
ENVIRONMENT VARIABLES
The TMPDIR environment variable can be used to specify a preferred directory for creating temporary files. If set, tempfile will attempt to use this directory first, unless the -t option is explicitly provided. If TMPDIR is not set, tempfile typically falls back to common temporary paths such as /tmp or /var/tmp.
EXIT STATUS
tempfile exits with a status of 0 upon successful creation of a temporary file or directory (or successful generation of a name with -n). A non-zero exit status indicates an error, such as a problem with permissions, disk space, or invalid options provided by the user.
HISTORY
tempfile has been a part of the util-linux project for many years, serving as a simple utility for generating unique temporary filenames in shell scripts. Its primary purpose was to address the challenge of securely creating temporary files while avoiding race conditions and name collisions, which were common issues with simpler methods. While effective for its time, its functionality has largely been superseded by the more advanced and flexible mktemp utility, which offers better portability and additional features. Consequently, tempfile is now officially deprecated, with mktemp being the recommended alternative for modern Unix-like systems.