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.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 12 comments… read them below or add one }
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.
Use logger command
createtempfiles.bash is missing… But article is still useful :)
Artem,
Thanks for the heads up! The post has beeb updated.
$ tempfile -ddoes not create a temporary directory. Instead, it requires a directory as an argument and creates a temporary file inside that directory.Thank’s. Obrigado ! Very good tutorial !
Hi
useful posts.
But i need to have filenames in sequence eachtime when i run the script in which i am creating the file.
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:
BASE=$(mktemp)
for a in {0..5}; do
cp $BASE $BASE.$a
done
That said, I really don’t understand why you’d care if they were in sequence.
This article is very useful: it helps me solve my problem. Thank you.
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.
Great summary. Thank you.
It helps writing shell script!
mktemp /path/of/dorectory/filename.XXXXXX
The more X’s you add the more random characters it adds.