Tutorial mktemp Linux Commands for Beginners (5 Examples)

Tutorial mktemp Linux Commands for Beginners (5 Examples)

Creating temporary files or directories is a common task performed by many Linux command line users. But did you know that there is a special command line tool – nicknamed mktemp – that allows you to do this?

In this tutorial, we will discuss the basics of commands using a number of examples that are easy to understand. But before we do that, it is worth mentioning that all the examples here have been tested on Ubuntu 20.04 LTS machines and on Debian 10.

Linux mktemp command

The mttemp command on Linux allows users to create temporary files or directories. Following is the syntax:

mktemp [OPTION]... [TEMPLATE]

And this is how the tools man pages explain it:

Create a temporary file or directory, safely, and print its name.  TEM?
       PLATE must contain at least 3 consecutive 'X's in last  component.   If
       TEMPLATE is not specified, use tmp.XXXXXXXXXX, and --tmpdir is implied.
       Files are created u+rw, and directories  u+rwx,  minus  umask  restric?

The following are some examples of Q&A styles that should give you a good idea of how the commands work.

Q1. How do you create a temporary file using mktemp?

Simple Simply run ‘mktemp’ sans any options. You will see a temporary file will be created in the / tmp / folder.


For example, in my case, the following output is produced:


Q2. How do you create a temporary directory using mktemp?

This can be done using the -d command-line option.

mktemp -d

For example, in my case, the following output is produced:


So you see, the name doesn’t reflect the fact that it’s a directory, but actually it is.

Q3. How do you give a special name to your temporary file / directory?

As you might have noticed now, mktemp assigns a random name to the temporary file or directory it creates. However, if you want, you can give a special name.

This tool offers a template that you can use. All you have to do is give a name, followed by three or more X as a continuation. This X is replaced by mttemp with a random character so that the last name of the file or directory turns out to be unique.

For example, I run the following command:

mktemp tempfileXXX

And the following file is produced:


Note that files generated this way are located in the current working directory, not in the / tmp / directory (which is the default behavior).

Q4. How do I add a suffix to the end of a file / directory name?

In the previous section, we discussed how to have a special name when using mktemp. However, you can only have the beginning of the name you want. What if you want the suffix of your choice too?

You will be glad to know this is also possible. You only need to use the –suffix option. The following are examples:

mktemp tempfileXXX --suffix=HTF

And here’s the name of the file produced by the aforementioned command:


So you can see the suffix you provided has been added to the file name.

Q5. How do you run a dry run with mktemp?

If you only want to put to display the name and not create a file / directory, then this can be done by using the -u command-line option.

mktemp -u


Depending on the type of work you do on the Linux command line, the command prompt can prove to be a practical and time-saving tool for you. Here in this tutorial, we have discussed a number of command-line options for bidding tools. To learn more, open the man page.

Related posts

How to Set Up WireGuard VPN Server and Client on Ubuntu 20.04


Using the ‘ldd’ command on Linux


How to Install Ruby On Rails on Ubuntu 20.04


How to Count Files in Directories on Linux


How to Change the Directory Name in Linux


How Linux makes data recovery easy


How to Set Up an iSCSI Storage Server on Ubuntu 20.04 LTS


How to Get Windows Terminal Fonts (Cascadia Code) on an Ubuntu Terminal


How to Install Apache ActiveMQ on Debian 10