Linux mktemp Command Tutorial for Beginners (5 Examples)
On this page
- Linux mktemp command
- Q1. How to create a temporary file using mktemp?
- Q2. How to create a temporary directory using mktemp?
- Q3. How to give a custom name to your temporary file/directory?
- Q4. How to add a suffix to the end of the file/directory name?
- Q5. How to just perform a dry run with mktemp?
- Conclusion
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 20.04 LTS machine and on Debian 10.
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?
tions.
The 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.
mktemp
For example, in my case, the following output was produced:
/tmp/tmp.Ko6d1aX84B
Q2. How to 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 was produced:
/tmp/tmp.Y8pySTB9ml
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:
tempfileEgh
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:
tempfilecVkHTF
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
Conclusion
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.