
mktemp Command in Linux
The mktemp is a command line utility in Linux that lets us create temporary files and directories. Temporary files are helpful for tasks like storing temporary data, managing logs, or creating unique session files in scripts.
The mktemp command ensure that the names of temporary files and directories do not conflict with existing files. This practice improves security and minimize the risk of unintentional overwriting.
Table of Contents
Here is a comprehensive guide to the options available with the mktemp command −
- What is mktemp Command in Linux?
- Syntax of mktemp Command
- mktemp Command Options
- Installation of mktemp Command
- Examples of mktemp Command in Linux
- How to Remove a Temp Files in Linux?
- Troubleshooting Common Issues with mktemp Command
What is mktemp Command in Linux?
The mktemp command creates temporary files or directories with unique names. It uses a user-provided template and replaces placeholders with random characters to ensure each name is unique. This is especially helpful in scripts and automation, where temporary storage is needed without worrying about duplicate file names.
For example, in shell scripts, unique temporary files prevent conflicts when the script runs multiple times or at the same time.
Syntax of mktemp Command
To use this command in Linux, you must follow the below-given syntax −
mktemp [OPTION] [TEMPLATE]
Here, OPTION represent different command options that modify the behavior of mktemp while
TEMPLATE is a file or directory template, where X characters are replaced by random characters to ensure uniqueness.
mktemp Command Options
The mktemp command offers different options to customize how temporary files and directories are created −
Options | Description |
---|---|
-d | It creates a temporary directory instead of a file. |
-u | It shows the name of a unique temporary file or directory without actually creating it. |
-p | It sets the location where the temporary file or directory will be created. If not mentioned, it uses /tmp by default. |
--suffix=SUFFIX | Appends a suffix to the temporary file's name. |
-q | Surpresses error messages if file or directory creation fails. |
--help | It shows the help page for the mktemp command. |
--version | Returns the version of the mktemp command. |
To learn more about the mktemp command and its usage, execute the following command −
man maketemp

Installation of mktemp Command
The mktemp command is usually pre-installed on most Linux systems. If it's missing, you can quickly install it using your package manager.
To install mktemp on Debian/Ubuntu-based distributions, use the following command −
sudo apt update sudo apt install coreutils
For installing mktemp on Fedora-based distributions, you can execute the following command −
sudo dnf install coreutils
Similarly, Arch users can execute the Pacman package manager to install coreutils package, including the mktemp command −
sudo pacman -S coreutils
You can check the availability of the mktemp command on your Linux system by checking its version −
mktemp --version
The output confirms that mktemp version 9.4 is installed on our system −

Examples of mktemp Command in Linux
Let's understand the command usage by going through the following examples −
Creating a Temporary File
Let's run the mktemp command to create a temporary file −
mktemp /tmp/tutorialspoint.XXXXXX
It creates a unique temporary file in the /tmp folder. The XXXXXX is replaced with random characters to make sure the file name is different each time, as shown in the following screenshot −

Creating a Temporary Directory
You can execute the mktemp command with the -d option to create a temporary directory instead of a file −
mktemp -d /tmp/tutorialsDir.XXXXXX
A new temporary directory has been successfully created with the mktemp command −

Specifying a Custom Directory
To create a temporary file in a specific directory, we can run the mktemp command with the -p option −
mktemp -p /home/user/tmp /tutorials.XXXXXX
This command creates a temporary tutorial file in /home/user/tmp instead of the default /tmp directory.
Creating a Temporary File with a Custom Suffix
To create a temporary file with specific suffix, use the --suffix options, as follows −
mktemp --suffix=.txt /tmp/textFile.XXXXXX
In this example, we append .txt at the end of temporary text file −

Generating a Unique Temporary File Name (Without Creating the File)
During debugging, Linux users may need a unique temporary file name without creating the file. In such cases, they can use the mktemp command with the -u option, as shown below −
mktemp -u /tmp/expTutorials.XXXXXX
The output confirms that the -u option outputs the name of a unique file without creating it −

Here, it is important to note that using the -u option is discouraged in most cases because it doesn't prevent name conflicts if another process creates the file with the same name later.
How to Remove a Temp Files in Linux?
mktemp doesn't delete temporary files automatically, as shown in the following screenshot −

You can use the rm command to remove them when they're no longer needed −
rm textFile.WzYbDz.txt

Troubleshooting Common Issues with mktemp Command
The mktemp command is simple and easy to use, however, you may still face some common problems. Let's go through the following list to see the common issues and how to fix them −
Template Does Not Contain Sufficient X Characters
To avoid the issue of insufficient X characters, make sure the template has at least six X characters to create a unique filename. For example, the "mktemp /tmp/tutorialspoint.XXXXX" command will work appropriately, but "mktemp /tmp/tutorialspoint.X" will not. This is because the second command has insufficient X characters.
Permission Denied When Creating Files or Directories
Make sure you have write permissions to the specified directory. For example, the /tmp directory is writable by all users, but a custom directory might not be.
Temporary Files Not Being Cleaned Up
mktemp does not clean up temporary files automatically. You can remove the temporary files using rm once they are no longer needed, or consider setting up automatic cleanup in your scripts.
rm /tmp/mytempfile.*
That's all about creating temporary files and directories in Linux.
Conclusion
The mktemp command is a useful command line tool for creating temporary files and directories in Linux. It ensures that the filenames are unique and prevents conflicts, which is especially useful in scripting and automation tasks.
With its simple syntax and various options, you can easily create temporary resources in any directory of your choice. In this article, we explained how to use the mktemp command to handle temporary files securely.