Temporary files and directories are very important: they can be used from shell scripts for example, to store information which are necessary to complete some tasks and can be safely removed once the work is done. In this tutorial we will see how to safely create temporary files and directories using the mktemp
utility on Linux.
In this tutorial you will learn:
- How to use mktemp
- How to provide a custom template for temporary file names
- How to specify an alternative directory for temporary files
- How to create a temporary directory
Software requirements and conventions used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Distribution-independent |
Software | mktemp |
Other | None |
Conventions | # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux-commands to be executed as a regular non-privileged user |
Introducing mktemp
The mktemp
utility lets us safely create temporary files and directories named using a predefined or optionally user-provided “template”. The utility is installed by default on all the major Linux distributions, usually as part of the “coreutils” package, so it is included even in minimal installations.
In its most basic usage it can be invoked without specifying any arguments or options. When used this way, it creates a temporary file in the /tmp
directory.
To safely create a temporary file or directory, its name should not collide with an already existing one. To make sure this doesn’t happen, mktemp
uses the following template:
tmp.XXXXXXXXXX
The
X
characters in the template are replaced by random characters and numbers. Let’s try to invoke mktemp
and see what result we obtain:
$ mktemp /tmp/tmp.too2NcMWqn
As you can notice, by default mktemp
creates a temporary file (to create a temporary directory we should use a specific option which we will see later), in the /tmp
directory using the aforementioned naming template.
The name of the created file is printed on standard output; this makes us able to use it as the value of variables we can use to reference it for subsequent operations: write something to it, or delete it once a script task is performed.
Providing a custom template for temporary file names
We just saw the default naming template that is used by mktemp
to safely create temporary files. If we want, however, we have the chance to provide our custom naming template: all we have to do is to invoke the application and pass our custom template pattern as argument. To be considered valid, the provided template must at least contain 3 X
characters, which, as we saw, will be replaced by random ones. If this rule is not respected an error is generated:
$ mktemp customtemplatename.XX mktemp: too few X's in template ‘customtemplatename.XX’
Specifying an alternative directory for temporary files
As we already said, if not specified otherwise, temporary files and directories are created by mktemp
in the /tmp
system directory. This makes sense since it is where temporary files are stored by default. In certain situations, however, we may want to specify a different path. We can basically do it in two ways:
- Using the
TMPDIR
environment variable - Using the
--tmpdir
option when invoking mktemp
Let’s explore these options.
Using the TMPDIR variable
Assigning a value to the TMPDIR
environment variable is recommended if we want to set an alternative path to be used for temporary files and directories without having to specify it each time we invoke the application. We can, for example, set the value of this variable in our ~/.profile
or ~/.bash_profile
files (depending on the shell we are using).
Suppose I always want to use the /customtempdir
directory for temporary files created by mktemp
. Inside the ~/.bash_profile
file I would append the following content:
export TMPDIR="/customtempdir"
As you may know, it is a convention to use capital letters for the name of environment variables, that’s why TMPDIR
is used. In the example above we assigned a value to the variable, but we also used the export
shell builtin, why? It is used to make a variable available in the environment of all the child processes of the shell. In the example above we exported and assigned a value to the variable in the same line, but we could also have written:
TMPDIR="/customtempdir" export TMPDIR
The specified directory should already exist and have the appropriate permissions set applied to it. It will not be created on the fly by
mktemp
, and it should be writeable by all those who should use it. This is evident if we examine the permissions applied to the default /tmp
directory:
$ ls -ld /tmp drwxrwxrwt. 22 root root 520 Dec 22 12:45 /tmp
As we can see from the output of ls
, the directory is owned by the root
user and the root
group, but is writable and explorable by everybody. The final t
in the permissions report produced by ls
, means that the sticky bit is set, so all the files created in the directory are modifiable only by their owners. Remember that we can easily set the sticky bit on a directory by running the following command:
$ sudo chown o+t /path/to/the/directory
To make the changes to our environment effective, we need to login an logout from the system, or use source
to immediately (and temporarily – the changes will be lost when we close the current shell) re-source the file (~/.bash_profile
, in this case):
$ source ~/.bash_profile
Once the variable is part of the environment, its value will be used by mktemp
as the destination directory in which to create temporary files and directories:
$ mktemp /customtempdir/tmp.JXuNpunTUm
Using the –tmpdir option
The other way we can specify an alternative directory in which temporary files and directories should be created, is by using the --tmpdir
option (-p
) at runtime. The option takes the path of directory we want to use as argument. To use the /customtempdir
directory this way, for example, we would run:
$ mktemp --tmpdir=/customtempdir
Or
$ mktemp -p /customtempdir
Creating a temporary directory
As we saw in the previous examples, by default the mktemp
utility creates temporary files . In some cases, however, we may want to create temporary directories instead. How can we do it? It is very simple: all we have to do is to invoke the utility with the -d
option, which is the short version of --directory
. The same pattern that is used for temporary file names is also used for directories:
$ mktemp -d /tmp/tmp.YKQDLww3kT
Closing thoughts
The ability to safely create temporary files and directories is sometimes needed, specially from shell scripts which should store temporary information. In this tutorial we saw how to perform such a task using the mktemp
utility on Linux: we saw what is the default template which is used to name temporary files and how to provide a custom one, how to specify in which directory they should be created, (the default being /tmp
), and finally we saw how to create temporary directories instead of regular files.