tempfile
Create temporary files or directories
SYNOPSIS
tempfile [OPTION]...
PARAMETERS
-d, --directory DIR
Create the temporary file in the specified directory DIR. By default, tempfile uses /tmp.
-e SUFFIX
The last few characters of the temporary filename are taken from SUFFIX. If not provided, the temporary filename has 6 random characters after the prefix.
-p PREFIX
Use PREFIX as the directory prefix. Implies -d and defaults to TMPDIR or /tmp if that's unset
-m MODE
Set the security mode of the file or directory to MODE (octal). Defaults to 0600 for files and 0700 for directories.
-t
Treat the option as creating a temporary directory instead of a file.
-q, --quiet
Suppress diagnostic messages.
-T
Interpret the options after this option as filenames.
--tmpdir DIR
Deprecated equivalent to the -d option.
--suffix SUFFIX
Deprecated equivalent to the -e option.
--prefix PREFIX
Deprecated equivalent to the -p option.
--help
Display help message and exit.
--version
Display version information and exit.
DESCRIPTION
The tempfile
command is a simple utility to create temporary files or directories. It is designed to be used in shell scripts to safely generate unique names for temporary storage. This prevents common security vulnerabilities associated with manually creating temporary files, such as race conditions and predictable file names. The tool ensures that the created files have proper permissions, and it is commonly used by developers and sysadmins who require secure temporary storage in scripts. Without the tool, the temporary files can be easily exploited and cause data breaches. The tool provides guarantees of uniqueness for the files and directories created, avoiding collision.
CAVEATS
tempfile
does not automatically delete the created temporary files or directories. Your script must handle the deletion when the files are no longer needed, typically using rm
or rmdir
. Failure to do so can lead to accumulating temporary files and exhausting disk space.
The command relies on the system's ability to generate random and unique names. While it significantly reduces the risk of collisions, a theoretical possibility always remains, especially under high-load conditions.
USAGE EXAMPLE
To create a unique temporary file in /tmp with a suffix '.log':tempfile -e .log
To create a unique temporary directory in the current directory with a prefix 'my_temp':tempfile -d . -t -p my_temp
To ensure only the owner has read write permissions on the temp file:tempfile -m 600
HISTORY
tempfile
is a relatively modern utility designed to address the security shortcomings of older methods of creating temporary files in shell scripts. It improves upon earlier approaches by providing guaranteed uniqueness and secure file permissions. The command's standardization across Linux distributions has made it a preferred choice for scripting. Its widespread usage aims to reduce the usage of unsafe methods for managing temporary files.