Home > File & Directory Management > mktemp

mktemp: Create Temporary Files/Directories

The mktemp command securely creates temporary files or directories with unique names. It is designed for use in scripts when temporary storage is needed, preventing name collisions and security issues.

Overview

mktemp generates temporary files or directories with unpredictable, unique names, thus preventing security vulnerabilities and naming conflicts. It is commonly used in shell scripts requiring a secure temporary space, and the path of the created file/directory is returned to standard output.

Key Features

  • Generates unique and unpredictable names
  • Prevents security vulnerabilities (e.g., race condition attacks)
  • Option to create temporary files or temporary directories
  • Suitable for shell script automation

Key Options

The mktemp command offers several key options to control how temporary files or directories are created.

Creation Type

Name Template

Generated command:

Try combining the commands.

Description:

`mktemp` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Demonstrates various ways to use the mktemp command to create temporary files and directories and utilize them in scripts.

Create a Basic Temporary File

mktemp

Creates a temporary file with a unique name in the default temporary directory (usually /tmp).

Create a Temporary Directory

mktemp -d

Uses the -d option to create a temporary directory.

Create a Temporary File with a Specific Pattern

mktemp /tmp/my_app.XXXXXX

Creates a temporary file using a specified path and pattern. The 'X' characters will be replaced by unique characters.

Using Temporary Files in Scripts and Automatic Deletion

TEMP_FILE=$(mktemp)
if [ -z "$TEMP_FILE" ]; then
  echo "Failed to create temp file"
  exit 1
fi
trap 'rm -f "$TEMP_FILE"' EXIT

echo "Temporary file path: $TEMP_FILE"
echo "Hello from mktemp!" > "$TEMP_FILE"
cat "$TEMP_FILE"
# $TEMP_FILE will be automatically deleted when the script exits.

Stores the path of the created temporary file in a variable and uses the trap command to automatically delete the file upon script termination.

Using Temporary Directories in Scripts and Automatic Deletion

TEMP_DIR=$(mktemp -d)
if [ -z "$TEMP_DIR" ]; then
  echo "Failed to create temp directory"
  exit 1
fi
trap 'rm -rf "$TEMP_DIR"' EXIT

echo "Temporary directory path: $TEMP_DIR"
echo "This is a test file." > "$TEMP_DIR/test.txt"
ls -l "$TEMP_DIR"
cat "$TEMP_DIR/test.txt"
# $TEMP_DIR will be automatically deleted when the script exits.

Stores the path of the created temporary directory in a variable and uses the trap command to automatically delete the directory upon script termination.

Tips & Precautions

Useful tips and points to be aware of when using mktemp.

Mandatory Cleanup After Use

mktemp creates temporary files but does not delete them automatically. You must explicitly delete the created files or directories using the `rm` command upon script termination. The `trap` command can be used to ensure automatic cleanup regardless of how the script exits.

  • File: `rm "$TEMP_FILE"`
  • Directory: `rm -rf "$TEMP_DIR"`
  • Automatic Cleanup: `trap 'rm -rf "$TEMP_PATH"' EXIT`

Security Considerations

mktemp is designed to be secure by preventing race condition attacks during temporary file creation. However, if you store sensitive information in the created files, ensure appropriate file permissions are set (e.g., `chmod 600 $TEMP_FILE`) to prevent access by other users.

When Using Templates

Template strings must contain at least three 'X' characters. A higher number of 'X' characters increases the uniqueness of the generated names.

  • Recommended: XXXXXX (6 or more)
  • Minimum: XXX (3)

Same category commands