Linux mktemp Command Tutorial for Beginners (5 Examples)

Creating temporary files or directories is a common task that many Linux command line users do. But do you know there's a dedicated command line tool - dubbed mktemp - that lets you do this?

In this tutorial, we will discuss the mktemp command basics using some easy to understand examples. But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.

Linux mktemp command

The mktemp command in Linux lets users create a temporary file or directory. Following is its syntax:

mktemp [OPTION]... [TEMPLATE]

And here's how the tool's man page explains 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?

Following are some Q&A-styled examples that should give you a good idea on how the mktemp command works.

Q1. How to create a temporary file using mktemp?

Simple. Just execute 'mktemp' sans any option. You'll see a temporary file will get created in the /tmp/ folder.


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


Q2. How to create a temporary directory using mktemp?

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

mktemp -d



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

Q3. How to give a custom name to your temporary file/directory?

As you might have observed by now, mktemp gives a random name to the temporary file or directory that it creates. However, if you want, you can provide a custom name.

The tool offers a template that you can use. All you have to do is to provide a name, followed by three or more X in continuation. These X's are replaced by mktemp with random characters so that the final name of the file or directory turns out to be unique.

For example, I executed the following command:

mktemp tempfileXXX

And the following file was produced:


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

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

In the previous section, we discussed how to have a custom name when using mktemp. But that way, you can have only the initial part of the name as desired. What if you want a suffix of your choice as well?

You'll be glad to know this is also possible. You just have to use the --suffix option. Following is an example:

mktemp tempfileXXX --suffix=HTF

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


So you can see the suffix you supplied has been added to the filename.

Q5. How to just perform a dry run with mktemp?

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

mktemp -u


Depending on the kind of work you do on the Linux command line, the mktemp command can prove to be a handy, time-saving tool for you. Here, in this tutorial, we have discussed several command line option the tool offers. To learn more, head to its man page.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

Suggested articles

0 Comment(s)

Add comment