-
-
Notifications
You must be signed in to change notification settings - Fork 2
Linux mktemp Guide
Mattscreative edited this page Dec 5, 2025
·
2 revisions
Complete beginner-friendly guide to mktemp on Linux, covering Arch Linux, CachyOS, and other distributions including temporary file creation, secure temp files, and temporary directories.
Basic usage:
# Create temporary file
mktemp
# Creates file in /tmp/
# Returns filenameAssign to variable:
# In script
TEMP_FILE=$(mktemp)
echo "Data" > "$TEMP_FILE"
# Use file
rm "$TEMP_FILE"System temp:
# Creates in /tmp/
mktemp
# Output: /tmp/tmp.XXXXXXSpecify directory:
# Custom location
mktemp /tmp/myfile.XXXXXX
# Creates in specified locationTemp directory:
# Create directory
mktemp -d
# -d = directory (creates directory)In script:
# Create temp directory
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR"
# Use directory
cd -
rm -r "$TEMP_DIR"Custom template:
# Template format
mktemp /tmp/myscript.XXXXXX
# X's replaced with random charactersMore randomness:
# More X's = more random
mktemp /tmp/file.XXXXXXXXXX
# More random charactersCheck installation:
# mktemp is part of coreutils
# Usually pre-installed
# Check mktemp
which mktempThis guide covered mktemp usage, temporary file creation, and secure temp files for Arch Linux, CachyOS, and other distributions.
- Bash Scripting Guide - Scripting basics
- File Operations - File management
-
mktemp Documentation:
man mktemp
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.