≡ Menu

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

Various methods exists to create a random temporary file name. This is useful if your application/shell scripting needs temporary unique file names.

Method #1: Use of $RANDOM bash shell variable

1) At shell prompt type command:
# echo $RANDOM

You will get random value every time. This variable can be use to create unique file name as demonstrated by createtempfiles.bash script.

1) Download/view createtempfiles.bash script

2) Save the script to your computer and execute it as follows:
$ chmod +x random1.bash
$ ./random1.bash


Use this method if your script needs more than two temporary files.

Method # 2 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 diretory 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.

Method # 3 Use of mktemp or tempfile utility

As name suggest both makes unique temporary filename. Just type mktemp at shell prompt to create it:
$ mktemp
$ tempfile

Make a unique temporary directory instead of a file using –d option to both of them
$ mktemp –d
$ tempfile –d

Both mktemp or tempfile provides the shell scripts facility to use temporary files in safe manner hence it is highly recommended to use them.

Share this on:

{ 14 comments… add one }

  • surender July 24, 2007, 6:03 am

    how can i record the log in the log file in shell script?
    like during running whatever the user is seeing, that should be recorded in log file.

  • nixCraft July 24, 2007, 7:52 am
  • Artem Nosulchik October 5, 2007, 8:17 am

    createtempfiles.bash is missing… But article is still useful :)

  • nixCraft October 5, 2007, 11:10 am


    Thanks for the heads up! The post has beeb updated.

  • Antti Kaihola September 24, 2008, 8:25 am

    $ tempfile -d does not create a temporary directory. Instead, it requires a directory as an argument and creates a temporary file inside that directory.

    • al0 August 12, 2015, 10:28 am
      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]
  • tnt2br October 30, 2008, 6:46 pm

    Thank’s. Obrigado ! Very good tutorial !

  • Anuj Aggarwal March 3, 2009, 10:27 am


    useful posts.
    But i need to have filenames in sequence eachtime when i run the script in which i am creating the file.

    • CWS April 30, 2013, 11:09 pm

      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.

  • Hai Vu May 21, 2009, 5:08 pm

    This article is very useful: it helps me solve my problem. Thank you.

  • David September 7, 2010, 9:49 pm

    After reading http://www.linuxsecurity.com/content/view/115462/151/ , I think some of your examples may be vulnerable to symlink attacks.

    $RANDOM (once in the filename) or $$ alone are not enough because it may be possible for an attacker to create symlinks for all filenames.

  • Yuki Matsukura September 12, 2011, 11:56 am

    Great summary. Thank you.
    It helps writing shell script!

  • Wesley April 19, 2013, 8:15 am

    mktemp /path/of/dorectory/filename.XXXXXX
    The more X’s you add the more random characters it adds.

  • Hal September 24, 2013, 9:04 pm

    Suggest that you list the preferred method first rather than last.

Leave a Comment

   Tagged with: , , ,