Signup/Sign In

How to Create a Temporary File or Directory in Linux

In Linux, if we want to create a temporary file or directory safely (in /tmp directory) and print its name, we use the mktemp command. It was first released in 1997 as a part of OpenBSD.

The mktemp (make temporary) command is used to create a temporary file or directory with a random extention/suffix safely and prints its name in STDOUT on Linux/Unix based operating systems.

  • mktemp stands for make temporary.
  • mktemp takes the given file name template and overwrites a portion of it to create a unique filename. (default format "tmp.XXXXXXXXXX")
  • All files and directories will be saved in the system's temporary directory (/tmp), therefore we do not need to clean up manually as it will be cleaned on the next restart of the system.
  • If we want to create a custom name then we have to add at least three consecutive 'X's at the end of the file and a custom created file. The custom-created file is saved in the current directory by default.
mktemp [OPTION]... [TEMPLATE]

Brief description of options available with the mktemp command.

Option Description
-d, --directory create a directory, not a file
-u, --dry-run do not create anything; merely print a name (unsafe)
-q, --quiet suppress diagnostics about file/dir- creating failure.
-p DIR, --tmpdir[=DIR] interpret TEMPLATE relative to DIR; if DIR is not specified, use $TMPDIR if set, else /tmp. With this option, TEMPLATE must not be an absolute name; unlike with -t, TEMPLATE may contain slashes, but mktemp creates only the final component
-t interpret TEMPLATE as a single file name component, relative to a directory: $TMPDIR, if set; else the directory specified via -p; else /tmp [deprecated]
--suffix=SUFF append SUFF to TEMPLATE; SUFF must not contain a slash. This option is implied if the TEMPLATE does not end in X.
--help display this help and exit.
--version output version information and exit.

mktemp command in Linux terminal

Create a temporary file with mktemp command in linux

This is a simple example using the mktemp command. After executing this command, a file is created with a random name in /tmp directory. This is just an empty file.

$ ls
$ mktemp
/tmp/tmp.1FZCeRRnEU
$ ls /tmp/tmp.1FZCeRRnEU -al
-rw------- 1 root root 0 Aug 21 08:51 /tmp/tmp.1FZCeRRnEU

Linux terminal: create a temporary file of a directory

Create a temporary directory with mktemp command in linux

for creating a temporary directory using the mktemp command with -d option. After executing this command a random empty directory in /tmp folder.

$ mktemp -d
/tmp/tmp.g14Jbnj2HE
$ ls /tmp/tmp.g14Jbnj2HE/ -al
total 8
drwx------ 2 root root 4096 Aug 21 08:54 .
drwxrwxrwt 1 root root 4096 Aug 21 08:54 ..

Linux terminal: mkdir temporary directory

Create temporary files or directories with a custom name and directory with mktemp command in linux

To create a custom name we have to add at least three consecutive 'X's at the end of the file and a custom created file. The custom-created file is saved in the current directory by default. You can use slashes in custom names to use a custom directory.

$ mktemp /tmp/abc-script.XXXXXX
/tmp/abc-script.UYuBjB
$ ls /tmp/abc-script.UYuBjB -al
-rw------- 1 root root 0 Aug 21 08:55 /tmp/abc-script.UYuBjB

Linux terminal: mktemp custom name

Create a temporary file with a specified extension with mktemp command in linux

In this example, using the --suffix option file extension is specified i.e., .txt after executing this command you can see that a new temporary file is created 'randomD4F.txt' with .txt suffix hereafter 'random' D4F is a random character generated by the system.

$ mktemp --suffix ".txt" randomXXX
randomclT.txt
$ mktemp --suffix ".txt" randomXXX -d
randomBID.txt
$ ls -al random*
-rw------- 1 root root    0 Aug 21 08:57 randomclT.txt

randomBID.txt:
total 8
drwx------ 2 root root 4096 Aug 21 08:57 .
drwxrwxrwx 3 root root 4096 Aug 21 08:57 ..

Linux terminal: mktemp suffix extension

Create temporary file for directory in specified directory with mktemp command in linux

Use -p or --tmpdir=, we can specify a custom $TMPDIR to save files in a specific directory. ie, here we specified a directory h as the temporary directory.

$ mktemp -p t
mktemp: failed to create file via template 't/tmp.XXXXXXXXXX': No such file or directory
$ mkdir t
$ mktemp -p t
t/tmp.zgmT4T8Z54
$ ls -al t
total 8
drwxr-xr-x 2 root root 4096 Aug 21 08:59 .
drwxrwxrwx 4 root root 4096 Aug 21 08:59 ..
-rw------- 1 root root    0 Aug 21 08:59 tmp.zgmT4T8Z54
$

Linux terminal: mktemp advance command with custom directory

Display the version information of mktemp command in linux

for checking the version information of the mktemp command execute mktemp --version after executing this command the version information of mktemp is prompt.

Linux terminal: mktemp version

Example: Display help of mktemp command in linux

In this tutorial, only an important option with a description is defined for checking more about the mktemp command execute mktemp --help this will prompt all options available in mktemp with a description.

Linux terminal: mktemp help or manual

Conclusion

In this tutorial, we covered how to create a temporary file or directory using the mktemp command with available options and suitable examples. mktemp command allows shell scripts to safely use temporary files. As usual shell scripts take the name of the program with the PID (process ID) as a suffix and use that as a temporary file name but this makes the job easier for a hacker.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.