“mktemp” is a command-line utility in Linux and Unix-like systems used to create temporary files or directories. The primary purpose of “mktemp” is to provide a convenient and safe way to create temporary files in shell scripts and other programs, to avoid potential race conditions and security issues that can arise when using simple file names based on the current time or process ID.
The “mktemp” command takes a template string as an argument, which specifies the desired file name or directory name pattern. The template string can contain X’s, which are replaced with unique characters to create a unique file or directory name. For example, if you use the template “/tmp/tmp.XXXXXX”, “mktemp” will replace the X’s with a unique string of characters to create a file or directory such as “/tmp/tmp.z48xd3”.
The “mktemp” command also provides options to control the permissions of the created file or directory, to specify the directory where the temporary file should be created, and to ensure that the file or directory is readable and writable only by the user who created it.
In general, “mktemp” is a useful tool for shell script developers and system administrators, as it provides a convenient and secure way to create temporary files and directories in scripts and other programs.
If you encounter the below error while running the command mktemp:
mktemp: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
Debian | apt-get install coreutils |
Ubuntu | apt-get install coreutils |
Alpine | apk add coreutils |
Arch Linux | pacman -S coreutils |
Kali Linux | apt-get install coreutils |
CentOS | yum install coreutils |
Fedora | dnf install coreutils |
OS X | brew install coreutils |
Raspbian | apt-get install coreutils |
mktemp Command Examples
1. Create an empty temporary file and print the absolute path to it:
# mktemp
2. Create an empty temporary file with a given suffix and print the absolute path to file:
# mktemp --suffix ".ext"
3. Create a temporary directory and print the absolute path to it:
# mktemp -d