Some programs need to write temporary files. Here is the usual way to construct a name for such a file:
The job of
make-temp-file is to prevent two different users or
two different jobs from trying to use the exact same file name.
This function creates a temporary file and returns its name. Emacs creates the temporary file's name by adding to prefix some random characters that are different in each Emacs job. The result is guaranteed to be a newly created file, containing text if that's given as a string and empty otherwise. On MS-DOS, this function can truncate the string prefix to fit into the 8+3 file-name limits. If prefix is a relative file name, it is expanded against
temporary-file-directory.(make-temp-file "foo") ⇒ "/tmp/foo232J6v"
make-temp-filereturns, the file has been created and is empty. At that point, you should write the intended contents into the file.
If dir-flag is non-
make-temp-filecreates an empty directory instead of an empty file. It returns the file name, not the directory name, of that directory. See Directory Names.
If suffix is non-
make-temp-fileadds it at the end of the file name.
If text is a string,
make-temp-fileinserts it in the file.
To prevent conflicts among different libraries running in the same Emacs, each Lisp program that uses
make-temp-fileshould have its own prefix. The number added to the end of prefix distinguishes between the same application running in different Emacs jobs. Additional added characters permit a large number of distinct names even in one Emacs job.
The default directory for temporary files is controlled by the
temporary-file-directory. This variable gives the user
a uniform way to specify the directory for all temporary files. Some
small-temporary-file-directory instead, if that is
nil. To use it, you should expand the prefix against
the proper directory before calling
This variable specifies the directory name for creating temporary files. Its value should be a directory name (see Directory Names), but it is good for Lisp programs to cope if the value is a directory's file name instead. Using the value as the second argument to
expand-file-nameis a good way to achieve that.
The default value is determined in a reasonable way for your operating system; it is based on the TMPDIR, TMP and TEMP environment variables, with a fall-back to a system-dependent name if none of these variables is defined.
Even if you do not use
make-temp-fileto create the temporary file, you should still use this variable to decide which directory to put the file in. However, if you expect the file to be small, you should use
small-temporary-file-directoryfirst if that is non-
This variable specifies the directory name for creating certain temporary files, which are likely to be small.
If you want to write a temporary file which is likely to be small, you should compute the directory like this:(make-temp-file (expand-file-name prefix (or small-temporary-file-directory temporary-file-directory)))
This function generates a string that might be a unique file name. The name starts with base-name, and has several random characters appended to it, which are different in each Emacs job. It is like
make-temp-fileexcept that (i) it just constructs a name and does not create a file, (ii) base-name should be an absolute file name that is not magic, and (iii) if the returned file name is magic, it might name an existing file. See Magic File Names.
Warning: In most cases, you should not use this function; use
make-temp-fileinstead! This function is susceptible to a race condition, between the
make-temp-namecall and the creation of the file, which in some cases may cause a security hole.
Sometimes, it is necessary to create a temporary file on a remote host or a mounted directory. The following two functions support this.
This function is similar to
make-temp-file, but it creates a temporary file as close as possible to
default-directory. If prefix is a relative file name, and
default-directoryis a remote file name or located on a mounted file systems, the temporary file is created in the directory returned by the function
temporary-file-directory. Otherwise, the function
make-temp-fileis used. prefix, dir-flag and suffix have the same meaning as in
make-temp-file.(let ((default-directory "/ssh:remotehost:")) (make-nearby-temp-file "foo")) ⇒ "/ssh:remotehost:/tmp/foo232J6v"
The directory for writing temporary files via
make-nearby-temp-file. In case of a remote
default-directory, this is a directory for temporary files on that remote host. If such a directory does not exist, or
default-directoryought to be located on a mounted file system (see
mounted-file-systems), the function returns
default-directory. For a non-remote and non-mounted
default-directory, the value of the variable
In order to extract the local part of the path name from a temporary
file-local-name could be used.