Shell scripting (BASH) : How to create temporary random file name

Posted on in Categories Linux, Shell scripting, Tips, UNIX last updated March 23, 2005

Sometimes you need to create a temporary file in your shell script. There are various methods exist to create a random temporary file name. It is quite useful if your application/shell scripting needs temporary unique file names.

Fig.01: How create a temporary file in linux unix bash/ ksh /zsh shell script
Fig.01: How create a temporary file in linux unix bash/ ksh /zsh shell script

Method # 1 Use of mktemp or tempfile utility

As the name suggest, both of the following commands create a unique temporary file or directory. Just type the mktemp at the shell prompt to create it a temp file or dir:
$ mktemp
Sample outputs


$ tempfile
Sample outputs:


Please note that the tempfile command is deprecated; you should use always use mktemp command instead. So to create a temp file:

tfile=$(mktemp /tmp/foo.XXXXXXXXX)
echo "a file: $tfile"

To create a temp dir:

tdir=$(mktemp -d /tmp/foo.XXXXXXXXX)
echo "a direcotry: $tdir"

How to make a directory

Make a unique temporary directory instead of a file using -d option. The syntax is:
$ mktemp -d

A shell script example

f="$(mktemp /tmp/myscript.XXXXXX)"
wget -q -O $f $s
echo "IPv4 address downloaded to '$f'.."
echo "Processing..."
# logic to do something on $f here
# Delete the temp file
rm -f "$f"

Rest of the following methods are insecure and do not use them in production. They are here for historical reasons only.

Method #2: Use $RANDOM bash shell variable

At shell prompt type the command:
$ echo $RANDOM
Sample outputs:


You can use it as follows:

echo "Working on temp $file ..."
echo "Deleting $file ..."
rm -f "$file"

Method # 3 Use of $$ variable

This is old and classic method. $$ shell variable returns the current running process this can be use to create unique temporary file as demonstrated in following script:
vi random2.bash

TFILE="/tmp/$(basename $0).$$.tmp"
ls > $TFILE
echo "See directory listing in $TFILE"

Save the script and execute as follows:
$ chmod +x random2.bash
$ ./ random2.bash

Use this method if your script needs only ONE temporary file.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

14 comment

    1. MKTEMP(1)                        User Commands                       MKTEMP(1)
             -d, --directory
                    create a directory, not a file
      So your comment seems to be incorrect.
      To specify directory to create a file in use either
                    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, TEM-
                    PLATE may contain slashes, but mktemp creates only the final component.
      -p DIR use DIR as a prefix; implies -t [deprecated]
    1. This is very necro, but for others who find this rather high ranking search and want to create some “temp” files in sequence, you could do something like:

      for a in {0..5}; do
      cp $BASE $BASE.$a

      That said, I really don’t understand why you’d care if they were in sequence.

Leave a Comment