mktemp

file managementLinux/Unix
The mktemp command is one of the most frequently used commands in Linux/Unix-like operating systems. mktemp Create a temporary file or directory

Quick Reference

Command Name:

mktemp

Category:

file management

Platform:

Linux/Unix

Basic Usage:

mktemp [options] [arguments]

Common Use Cases

    Syntax

    mktemp [OPTION]... [TEMPLATE]

    Options

    Option Description
    -d, --directory Create a directory instead of a file
    -u, --dry-run Do not create anything; merely print a name
    -q, --quiet Suppress error messages about file or directory creation failure
    --suffix=SUFFIX Append SUFFIX to TEMPLATE; SUFFIX must not contain a slash
    -p DIR, --tmpdir[=DIR] Interpret TEMPLATE relative to DIR; if DIR is not specified, use $TMPDIR if set, else /tmp. With this option, TEMPLATE must not be an absolute name
    -t Interpret TEMPLATE as a single file name component, relative to a directory: $TMPDIR, if set; else the directory specified via -p; else /tmp
    --help Display help information and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the mktemp command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    mktemp
    Create a temporary file with a random name in /tmp and print the filename.
    mktemp -d
    Create a temporary directory in /tmp and print the directory name.
    # Advanced Examples Advanced
    mktemp /tmp/test.XXXXXX Create a temporary file in /tmp with prefix "test." and random suffix. mktemp -d -p /var/tmp prefix.XXXXXX Create a temporary directory in /var/tmp with prefix "prefix." and random suffix. mktemp --suffix=.txt Create a temporary file with .txt extension. mktemp -u Generate a temporary filename but don't create the file. dir=$(mktemp -d) && echo "Working in $dir" && cd "$dir" Create a temporary directory and change to it. trap 'rm -rf "$TMPDIR"' EXIT && TMPDIR=$(mktemp -d) Create a temporary directory that will be removed when the script exits. tmpfile=$(mktemp) && echo "data" > "$tmpfile" && process "$tmpfile" Create a temporary file, write data to it, and process it. mktemp --tmpdir=/home/user XXXXXX Create a temporary file in a specific directory.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The 'mktemp' command is a utility for safely creating temporary files and directories in a secure manner. It generates unique filenames and ensures that files are created with proper permissions to prevent security vulnerabilities such as race conditions and other forms of attacks that can exploit predictable temporary file names. Key features of the mktemp command: 1. Secure File Creation: mktemp creates files with permissions that allow only the current user to read and write them (typically mode 0600), protecting sensitive data from other users on the system. 2. Unique Naming: The command generates uniquely named files by replacing the 'X' characters in a template with random characters, ensuring that filenames cannot be predicted or guessed. 3. Directory Support: With the -d option, mktemp can create temporary directories instead of files, useful for operations that require multiple temporary files. 4. Customizable Locations: While /tmp is the default location, mktemp allows specifying alternate directories for temporary files, which is useful when /tmp might be too small or when security policies require using specific locations. 5. Template-Based Naming: The command accepts a template argument that controls the naming pattern of the temporary file, allowing for recognizable prefixes while still ensuring uniqueness. 6. Path Generation Only: With the -u option, mktemp can generate a unique filename without actually creating the file, which is useful for scripts that need to know a filename in advance but will create the file differently. 7. Error Handling: mktemp provides clear exit codes and error messages, making it reliable for use in scripts where proper error handling is important. Common use cases for mktemp include: - Creating temporary files for storing intermediate data in shell scripts - Setting up temporary working directories for complex operations - Generating unique filenames for log files or other output - Creating secure locations for processing sensitive data - Avoiding filename collisions in multi-user or multi-process environments - Implementing cleanup routines that reliably remove temporary files when scripts exit It's worth noting that properly using mktemp helps avoid common security issues associated with temporary files, such as symlink attacks, where an attacker might replace a predictable temporary file with a symbolic link to a sensitive system file, potentially causing a privileged process to overwrite it. In shell scripts, mktemp is often used with the 'trap' command to ensure that temporary files and directories are cleaned up when the script exits, maintaining system cleanliness and preventing temporary file accumulation.

    Related Commands

    These commands are frequently used alongside mktemp or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the mktemp command works in different scenarios.

    $ mktemp
    View All Commands