1. Introduction

The Linux temporary directory follows the convention of UNIX in general and resides at /tmp, which is the standard temporary directory path in POSIX. While we can change its underlying partition, how we enforce a new temporary path depends on the context.

In this tutorial, we explore ways to change the Linux temporary directory path. First, we discuss a standard method to tell an application it should store its temporary data elsewhere. After that, we turn to two more direct methods for changing what and where /tmp is.

Importantly, we don’t explicitly talk about the Filesystem Hierarchy Standard (FHS) paths /var/tmp and /usr/tmp, although some of the methods discussed should work for them as well.

We tested the code in this tutorial on Debian 11 (Bullseye) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments.

2. Temporary Directory Variables and Location

The default Linux temporary directory path is /tmp. Still, we might be able to influence that in certain environments by setting variables in the shell.

There are three main environment variables that often dictate the current path applications should use for temporary data:

  • $TEMP
  • $TMP

Let’s see which variables affect our environment by setting each with export and testing:

$ export TMP=/home/baeldung/temp
$ mktemp
$ export TEMP=/home/baeldung/temp
$ mktemp
$ export TMPDIR=/home/baeldung/temp
$ mktemp

Notably, only the export of $TMPDIR changes the behavior of mktemp.

Actually, some interpreters, such as Python, employ all of the above to deduce a system’s temporary directory. Yet, even Python defines a priority:

  1. $TMPDIR
  2. $TEMP
  3. $TMP
  4. /tmp

So, similar to the POSIX mktemp, the interpreter prioritizes $TMPDIR over others, including the default. In fact, this is expected as $TMPDIR is in the POSIX standard as well:

This variable shall represent a pathname of a directory
made available for programs that need a place to create
temporary files.

In other words, although Python is just an example, some applications may employ $TEMP and $TMP, but most should check and adhere to $TMPDIR as the standard way to change the path of the temporary directory.

Now, let’s see what we can do about the applications that don’t use any of the above.

3. Change the Temporary Directory Location

Like other Linux filesystem objects, directories can be pointed to by links. Although with potential side effects, we can even replace /tmp with a link, switching up the actual directory or the reference path.

Of course, we can always link back to /tmp.

For example, an application may use /var/app/tmp instead of the system’s default temporary directory. In this case, we can avoid having to change environment variables by using ln:

$ mv /var/app/tmp/* /tmp
$ rm --recursive --force /var/app/tmp
$ ln --symbolic /tmp /var/app/tmp

There are several steps to this process:

  1. move all data from /var/app/tmp to /tmp, being careful when replacing
  2. remove /var/app/tmp
  3. recreate /var/app/tmp as a symbolic link (symlink) to /tmp

This way, our application should be able to directly employ /tmp without further modifications. One main drawback of this method is the possibility of the application to delete the /var/app/tmp directory and recreate it.

Alternatively, we can carefully delete /tmp and recreate it as a link, which points elsewhere:

$ mkdir /xtmp
$ chmod 1777 /xtmp
$ chown root:root /xtmp
$ cp /xtmp/* /tmp
$ rm --recursive --force /tmp
$ ln --symbolic /xtmp /tmp

This more complex process involves multiple commands:

  1. create a new directory /xtmp
  2. make the permissions of /xtmp like the permissions as a temporary directory
  3. remove the original /tmp (dangerous)
  4. recreate /tmp as a symbolic link to /xtmp

Still, there are several pitfalls to this approach:

  • /tmp is often a mount point for a separate partition, requiring further steps involving /etc/fstab
  • /tmp must have the correct permissions to avoid issues
  • depending on the time the partition behind /xtmp is mounted, earlier boot services may fail
  • using a link instead of /tmp may cause problems for some applications

If we consider all of the above, this is still a viable solution.

4. Summary

In this article, we looked at changing the Linux temporary directory path.

In conclusion, as long as they check them, we can tell applications to use another path for storing temporary files via environment variables. Still, we always have the alternative of changing what points to /tmp and where /tmp itself points.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.