# Make a temporary file with Bash When you're programming in the Bash scripting language, you sometimes need to create a temporary file. For instance, you might need have an intermediary file you need to commit to disk so you can process is it with another command. It's easy to create a file such as `temp` or anything ending in `.tmp`, but then again those are names that are just as likely to be generated by some other process, so you could accidentally overwrite an existing temporary file. And besides that, you shouldn't have to expend mental effort on coming up with names that seem unique. The `mktemp` on Fedora-based systems, and `tempfile` on Debian-based systems, are specially designed alleviate that burden my making it easy to create, use, and remove unique files. ## Creating a temporary file Both `mktemp` and `tempfile' create a temporary file as their default action, and print the name and location of the file as output. ```bash $ tempfile /tmp/fileR5dt6r $ mktemp /tmp/tmp.ojEfvMaJEp ``` Temporary files are, logically, placed into the `/tmp` directory unless you specify a different path. For `mktemp`, use the `-p` option: ```bash $ mktemp -p ~/Demo /home/tux/Demo/tmp.i8NuhzbEJN ``` For `tempfile`, use the `--directory` or `-d` option: ```bash $ tempfile --directory ~/Demo/ /home/sek/Demo/fileIhg9aX ``` ## Finding your temporary file The problem with using an auto-generated temporary file is that you have no way of knowing what its name is going to be. That's why both commands return the generated file name as output. If you're using an interactive shell like Konsole or GNOME Terminal or [rxvt](https://opensource.com/article/19/10/why-use-rxvt-terminal), you can use the filename returned to your terminal to interact with the file. However, if you're writing a script, then there's no way for you to intervene by reading the name of the file and using it in following commands. The authors of `mktemp` and `tempfile` thought of that problem, and there's an easy fix. In a terminal, output is sent to a stream called *stdout*, and you can capture stdout in a variable by setting a variable to the results of a command launched in a subshell: ```bash $ TMPFILE=$(mktemp -p ~/Demo) $ echo $TMPFILE /home/tux/Demo/tmp.PjP3g6lCq1 ``` Use `$TMPFILE` when referring to the file, and it's the same as interacting directly with the file itself. ## Create a temporary directory with mktemp The `mktemp` command can alternately create a directory instead of a file: ```bash $ mktemp --directory -p ~/Demo/ /home/tux/Demo/tmp.68ukbuluqI $ file /home/tux/Demo/tmp.68ukbuluqI /home/tux/Demo/tmp.68ukbuluqI: directory ``` ## Customize temporary names Sometimes you might want an element of predictability in even your pseudo-randomly generated filenames. You can customize the names of your temporary files with both commands. With `mktemp`, you can add a suffix to your filename: ```bash $ mktemp -p ~/Demo/ --suffix .mine /home/tux/Demo/tmp.dufLYfwJLO.mine ``` With `tempfile`, you can set a prefix and a suffix: ```bash $ tempfile --directory ~/Demo/ \ --prefix tt_ --suffix .mine /home/tux/Demo/tt_0dfu5q.mine ``` ## Tempfile as touch With `tempfile`, you can also set a custom name: ```bash $ tempfile --name not_random not_random ``` When you use the `--name` option, it's absolute, ignoring all other forms of customization. In fact, it even ignores the `--directory` option: ```bash $ tempfile --directory ~/Demo \ --prefix this_is_ --suffix .all \ --name not_random_at not_random_at ``` In a way, `tempfile` can be a substitute for `touch` and `test`, because it refuses to create a file that already exists. ```bash $ tempfile --name example.txt open: file exists ``` The `tempfile` command isn't installed on all Linux distributions by default, however, so were you to use it as a hack around `test` in a script, you must ensure that it exists on the distribution running your script. ## Install The `mktemp` command is included in [GNU Core Utils](https://www.gnu.org/software/coreutils/), which is included in major distributions by default (it's the same package that contains `chmod`, `cut`, `du`, and other essential commands.) The `tempfile` command is included in the Debian Utils package, installed by default on most Debian-based istributions as well as Slackware Linux. Temporary files are convenient because there's no confusion about whether they're safe to delete. They're temporary, meant to be used as needed and discarded without a second thought. Use them when you need them, and clear them out when you're done.