1. Overview

In this tutorial, we’ll show how to create temporary files when using Linux. We’ll start by explaining the purpose of temporary files, after which we’ll continue with the usage of the mktemp utility available on most Linux systems. Then, we’ll finish off with a POSIX compatible solution.

2. Use-Cases for Temporary Files

When we write scripts, it’s often useful to generate intermediate temporary files. These scripts will create these files and ultimately delete them when they have completed running.

There are several reasons we may want to create temporary files in our scripts, including:

  • Passing information to another process/script
  • Storing some debug information in case the script crashes
  • Keeping some data aside before doing manipulations on it

As a side note, it’s good practice to create these temporary files in the /tmp directory.

This directory is usually created by the operating system using the tmpfs filesystem. By using this type of filesystem, the directory can be cleaned up by unmounting it or by rebooting the machine.

3. Creating a Temporary File in Linux

3.1. Default Usage

Most Linux distributions provide the convenient mktemp utility, which permits us to effortlessly create a temporary file in Linux.

Running mktemp with no arguments will create a temporary file in /tmp and show the file’s path in the terminal’s output:

$ mktemp
/tmp/tmp.Dr6IfAc2HN

3.2. Using Templates

Sometimes, we may want to give a prefix to our temporary file to make it easier to detect in the /tmp directory.

As an example, let’s imagine we have a script named foo, and that we want temporary files created by foo to have the format /tmp/foo-XXXXX.

To achieve this, we’ll pass a template argument to mktemp:

$ mktemp /tmp/foo-XXXXX
/tmp/foo-J5BMk

The template requires a minimum of three ‘X’ template characters to be specified.

mktemp provides many more options for templating, including adding a suffix and specifying the directory in which it should create the file.

3.3. Accessing the File

If we create a temporary file in a shell script, we usually will want to access that file. To do so, we can assign the path to a variable.

Let’s see an example that shows how we can use the file we just created in our script:

TMPFILE=$(mktemp /tmp/foo-XXXXX)
echo "Hello world" >> $TMPFILE  # Append some text to the file
cat $TMPFILE                    # Print the contents of the file
rm $TMPFILE                     # Delete the file

Remember, each time we run the script, it will create a new file.

3.4. Using the trap Command to Delete the File

When keeping big temporary files, we might want to do some housekeeping in case our script fails.

We can do this using the trap command, which allows us to run some code when an exit code is detected:

TMPFILE=$(mktemp /tmp/foo-XXXXXX)
trap "rm -f $TMPFILE" EXIT

# Our smart code

In the above example, the code to delete TMPFILE runs whenever the EXIT signal is detected.

By entering trap -l at the command line, we’ll find a list of the available signals that can be used by the trap command.

4. POSIX Compatible Version

Unfortunately, the mktemp utility isn’t always available, as it’s not part of the POSIX specification.

Instead, POSIX specifies the mkstemp function as part of the C API:

The mkstemp(template) function shall create a regular file with a unique name derived from template and return a file descriptor for the file open for reading and writing.

This function is implemented as a macro by the GNU M4 macro processor. The GNU M4 macro processor copies every input text, after expanding any macro tokens in it, to the output.

We can use this tool to create our homebrew version of the mktemp utility:

$ echo 'mkstemp(/tmp/foo-XXXXXX)' | m4
/tmp/foo-dormh3

This version is POSIX compatible and behaves exactly like the mktemp utility:

TMPFILE=$(echo 'mkstemp(/tmp/foo-XXXXXX)' | m4)
echo "Hello world" >> $TMPFILE # Append some text to the file
cat $TMPFILE                   # Print the contents of the file
rm $TMPFILE                    # Delete the file

5. Conclusion

Temporary files are an essential tool when scripting in Linux. They provide an easy way to pass information between processes, store debug information or, save data before manipulating it.

Most Linux distributions provide the mktemp utility, which provides a user-friendly gateway to generate temporary filenames.

Since mktemp is not POSIX compliant, we can create a POSIX compliant version using the GNU M4 macro processor. This tool can expand the mkstemp() function specified by the C API.

guest
0 Comments
Inline Feedbacks
View all comments