Using mktemp Command to Create Temporary Files and Directories
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
mktemp command is used in Linux and BSD derivative operating systems to create temporary files or directories. The temporary filename and directories can be named using a user-defined “template”. This utility is installed by default on major Linux distributions.
mktemp varies from a traditional method of naming files using the PID in scripts. The use of
mktemp command allows a naming convention in a script that can be controlled by the script, and is more unlikely to collide with an existing temporary file, thus avoiding inadvertent temp file overwriting, or other undesirable collisions.
This guide demonstrates where
mktemp directives can be useful, and how to use templates to direct the formation of the file names.
Linux Temporary File and Directories: Use Cases
Temporary files created by
mktemp are owned by the user; commonly called from scripts to store data, in and among apps used within the script. It is a good practice to create temporary files in the
/tmp directory, which is created by the operating system.
mktemp command is different from the
mktemp creates files or directories that are flexible. It has features to ensure the file names are unique, generated randomly, and reduces accidental overwrite, or incorrect manipulation of an existing file, or directory. The
touch command modifies the timestamp of a file and is traditionally used to create permanent files, while
mktemp is used to create temporary files.
A script or independent invocation of
mktemp can also use a template that forms the names of the temp files and/or directories started by
Use Case #1 - Create A Simple Temporary File
mktemp with no arguments creates a random file in the
/tmp directory. The
/tmp directory is the default directory for file creations unless specified otherwise.
Use Case #2 - Create A Simple Temporary Directory
mktemp command with the
-d argument creates a temporary directory in the
mktemp command generates a random name that can be customized by passing a template argument. The template requires a minimum of three ‘X’ characters to be specified which indicates the places to be occupied by random characters.
tmp_dir=$(mktemp -d -t test-XXXX) echo $tmp_dir
mktemp command uses the currently set environment variable
$TMPDIR to place a new temporary directory. The default
$TMPDIR value found by most shell scripts is the
/tmp directory, or the other choice is the
/var/tmp directory. The difference between
/var/tmp is that the data stored in
/var/tmp directory is preserved between reboots and is more persistent than the data in
/var/tmp is not usually subjected to
systemctl extensions that may control temporary files.
How mktemp Is Used By System Administrators
mktemp command declares an explicit file or directory that is meant to be temporary. Although the
/tmp directory is often used for temporary files, applications used by multiple users and/or similar processes may create files using the same filename. For example, a single process can execute independently to create a file that can read, write, update, or delete the same file. This may cause a filename conflict requiring an administrative effort to rectify the problem. But, when a script calls
mktemp, a unique filename is created that is unlikely to collide with an existing filename or directory having the same name.
You can use
mktemp independently or within scripts to:
- Create temporary files and/or directories
- Test whether temporary file and/or directory creation is possible
- Create file or directory names that follow a specific identity pattern with added random characters generated from an invocation-assigned pattern
- Change the
TMPDIRenvironmental variable to a custom directory other than the default
/tmp, with an optional pattern generated from an invocation-assigned pattern.
- Keep temporary files and directories protected as to the ownership of the user rights spawning the invocation of the command, so that other uses of the temporary files and directories created are protected from other scripts, processes, and filename collision caused by other users.
How to Create a Temporary File
mktemp syntax is described in the man(mktemp) pages. Most POSIX-compliant systems use the same syntax as shown in the table below:
|Makes a temporary file in the default temporary directory|
|Display mktemp version then exit|
|Make a directory in the default temporary directory|
|Perform execution of the command quietly, meaning without success or failure messages|
|Make a temporary filename, using a template, to be located in |
How to Create a Temporary Directory
mktemp is invoked with the
-d argument, it creates a directory in
/tmp. If there is an environmental variable
$TMPDIR, then it creates a directory in the
How to Create a Temporary File Template
A temporary file template can be used to differentiate the source or criteria by naming the file with a template. The
mktemp template uses the letter “X” to be replaced by a random character in place of the “X” when used as an argument. Up to ten “X” characters in a row, for a total of ten places can be randomized in this way.
The following command is used to create a file template, where “X” is replaced by randomized characters:
mktemp -t random-XXXXXXXX
The following command which uses the
-d argument to
mktemp produces a directory in the same way.
mktemp -d random-XXXXXXXX
You can view the above newly created temporary directory using the
ls -la command.
You can also add a suffix to a template while creating the directory as shown in the command below:
mktemp -d --suffix TODAY
The TMPDIR Environment Variable
TMPDIR environmental variable enables you to specify a different path for you to store the temporary files. It is stored in a list that is available to applications and shell scripts. The
TMPDIR variable permits many applications to know where the administrator has designated the storage of temporary directories, especially if the designation varies from the default use of the
/tmp directory. The
/tmp directory in some instances may be placed on special media like an SSD for speed purposes. To understand more on environment variables, see the Linode’s guide on Setting and Using Linux Environment Variables.
On some Linux systems, the
TMPDIR file is called or declared by
systemd-tempfiles, a daemon that can be set to periodically clean files by creation date, or other attributes not covered in this guide.
TMPDIR variable is changed, its value may only survive for the current session of the user or PID. When you reboot the system, it may default to
/tmp, or the setting called by the
systemd-tempfiles.conf file. When you restart a system or a session, or any other event, the
TMPDIR is restored to its previous value.
mktemp --tmpdir argument changes the destination relative to the value set by the
$TMPDIR value set in the environment.
In the example command below, the
$TMPDIR value is changed to a subdirectory, and
mktemp applies its files to the new path:
Template and other arguments could also be added to change the
$TMPDIR value. An example of a date-codified directory is shown below.
root@localhost:/home# tmpdir=$(mktemp -d -t ci-$(date +%Y-m-%d-%H-%M-%S)-XXXXXXXXXX) root@localhost:/home# echo $tmpdir
How to Delete Your Linux System’s Temp Files
Cleaning the temporary files depends on the Linux version you are using. Current Linux systems using systemd use a process called systemd-tempfiles. Depending on the system version, and its implementation, the files, and process used for cleaning the temporary files, and directories have different configurations.
It is a good practice to delete the temporary files in the
/tmp directory of your system frequently. This takes up unnecessary space that could be used for other data or processes. Generally, files in the
/tmp directory are removed by your system after every reboot.
/var/tmpdirectory are usually preserved between system reboots and are made available to the programs that require temporary files. The data stored in the
/var/tmpis more persistent that the data in the
The following section describes the different ways in which you can delete temporary files from your system.
For the currently logged-in user, where the user has no other active processes, the
/tmp directory is deleted by invoking the following command:
rm -rf /tmp
This deletes all
/tmp files recursively through subdirectories and forces the deletion of all files for which the user has privileges. If the user is the root or sudo, then all files may be deleted, and this can be disruptive. Depending on the currently logged-in user rights, the
rm -rf /tmp command may destroy files and directories that are otherwise in use.
You can also use the prebuilt
find command that is available in almost every Linux distribution. The
find command allows you to find the files and directories that satisfy a specific condition. For example, the following
find command finds and deletes all the temp files in the
sudo find /tmp -type f -delete
trap command can be used to manage the deletion of files made within a script. When the shell has finished its execution, the
trap command allows the specified temporary files to be deleted.
For example, if your script creates a temporary file and you want to delete it at each place where you exit your script, you can include a
trap command at the start of your script that deletes the file on exit:
tempfile=/tmp/tmpdata trap "rm -f $tempfile" EXIT
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on