1. Overview

Many times when we’re writing an application, we need a temporary file or directory. mktemp provides a system-based way to create a valid and unique temporary file or directory from a script or application.

In this tutorial, we’ll present a way to safely and reliably create temporary files and directories on Linux using mktemp.

2. Creating Temporary Files

Let’s say we’re starting a new Bash script to create and populate some temporary files:

#!/bin/bash

for f in 1 2 3
do
  cat "My contents of $f" >> "file_$f.txt"
done

This will create the files in whatever directory we’re executing our script from:

-rw-r--r--  1 alex  staff     24 Feb 13 15:38 file_1.txt
-rw-r--r--  1 alex  staff     24 Feb 13 15:38 file_2.txt
-rw-r--r--  1 alex  staff     24 Feb 13 15:38 file_3.txt

Using this, we could run into a couple of issues. First, to run without error, we’d need to have write permissions in the current directory. A second problem could arise with multiple instances of our program. There is a possibility that each instance would clash or overwrite the files. To avoid the above issues, let’s use mktemp for our temporary files.

3. Using mktemp for Temporary Files

Let’s update our script to use mktemp:

#!/bin/bash

for f in 1 2 3
do
  file=$(mktemp)
  echo "Writing file $file"
  echo "My Contents" >> $file
done

Executing it, we see:

$ ./example.sh 
Writing file /tmp/tmp.f3LkRe1i
Writing file /tmp/tmp.gxknRBeE
Writing file /tmp/tmp.lEzSU838

By using mktemp, we can create unique temporary files. Also, we can ensure that we will get the same functionality on different systems. mktemp will use the system-defined temporary directory. Running the script again, we can see three new, unique files generated:

$ ./example.sh 
Writing file /tmp/tmp.pf60Klk6
Writing file /tmp/tmp.qjosdpxw
Writing file /tmp/tmp.jh8LWOxU

So now we have three temporary files with unique names, but we have lost our naming scheme. Fortunately, mktemp has two options that will allow us to customize our files a bit more.

4. Using a Name Template

Using the -t option with mktemp allows us to define a prefix for the temporary files. This gives us a different way to add some organization to our temporary files. Let’s update our script to try it out:

#!/bin/bash

for f in 1 2 3
do
  file=$(mktemp -t file_${f})
  echo "Writing file $file"
  echo "My Contents" >> $file
done

Note that in this case, the ${f} is not required to use -t. It’s to get our loop variable into our file name template.

Let’s run the script:

$ ./example.sh 
Writing file /tmp/file_1.YJkLELJh
Writing file /tmp/file_2.fluURpHx
Writing file /tmp/file_3.vGEiJ7Kt

This gives us a similar name scheme in unique files but still isn’t quite what we’re looking for.

5. Using mktemp for a Temporary Directory

To keep our name scheme but also take advantage of a unique temporary location, let’s try using mktemp to create a temporary directory. It gives us a process-specific place to put our temporary files. It also enables us to clean up after ourselves by deleting the whole directory at the end instead of keeping track of all of the individual files. Let’s update our script to create a temporary directory (using -d) and write our files with the original names:

#!/bin/bash

dir=$(mktemp -d)
for f in 1 2 3
do
  file=file_$f.txt
  echo "Writing file $file to dir $dir"
  echo "My Contents" >> "$dir/$file"
done

This creates our original file names but in a unique, temporary location:

$ ls -lt /tmp/tmp.qFwNKUqt
total 24
-rw-r--r--  1 alex  staff  12 Feb 13 16:50 file_3.txt
-rw-r--r--  1 alex  staff  12 Feb 13 16:50 file_2.txt
-rw-r--r--  1 alex  staff  12 Feb 13 16:50 file_1.txt

We could then use rm -r $dir at the end of our script to remove the directory when we’re done. Alternatively, the system handling of temporary files will eventually clean it up.

6. Conclusion

In this tutorial, we presented a way to safely create temporary files and directories using mktemp.