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

by nixcraft on March 23, 2005 · 10 comments

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

Output:
/tmp/0.24101.txt
/tmp/0.28872.txt
/tmp/0.8457.txt
/tmp/0.18006.txt
/tmp/0.29528.txt

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

#!/bin/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
Output:
/tmp/tmp.IAnO5O
OR
$ tempfile
Output:
/tmp/IAnO5O

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.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

We're here to help you make the most of sysadmin work. So, subscribe!

{ 10 comments… read them below or add one }

1 surender July 24, 2007

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.

Reply

2 vivek July 24, 2007
3 Artem Nosulchik October 5, 2007

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

Reply

4 vivek October 5, 2007

Artem,

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

Reply

5 Antti Kaihola September 24, 2008

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

Reply

6 tnt2br October 30, 2008

Thank’s. Obrigado ! Very good tutorial !

Reply

7 Anuj Aggarwal March 3, 2009

Hi

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

Reply

8 Hai Vu May 21, 2009

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

Reply

9 David September 7, 2010

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.

Reply

10 Yuki Matsukura September 12, 2011

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

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 9 + 11 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: