What is the best way to create a unique file name for temporary files?
...Mike
'date +%s' or using $RANDOM if your shell allows and you create > 1Quote:> What is the best way to create a unique file name for temporary
> files?
Michael Heiming
>...Mike
HTH - Maurice
--
__ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/ \ Laws are silent in time of war.
\__/ Cicero
Esperanto reference / http://www.alcyone.com/max/lang/esperanto/
An Esperanto reference for English speakers.
...Mike
Quote:> What is the best way to create a unique file name for temporary files?
> ...Mike
In particular, in a security-critical application it can be
relatively easily predicted by a malicious attacker,
and in a heavily-used machine two processes using it might
accidentally hit on the same name ($RANDOM may well be same
for each instance of the process, so if two of them run within
same second...)
That may or may not matter in your case (like, if you are creating the
in a protected directory and you know there can't be two instances at
the same time), but if you are using a public directory like /tmp and
letting others run it, you might wish to do it more carefully.
The multiple-instance problem can be handled simply by adding
process ID ($$) to the string, but it won't help against
malicious attackers.
If your system has a working mktemp you could use it (but be
warned, quite a few mktemp's are broken and vulnerable to
attacks), otherwise here's a sample code that works with
Korn shell:
n=0
set -o noclobber
until MYTMP=/tmp/mytmp$$.$n; >$MYTMP ;do
n=$(( n+1 ))
done 2>/dev/null
set +o noclobber
Basically that keeps incrementing the suffix number until
it finds a free filename, and then creates it in same atomic
operation, so no other process can intervene.
With POSIX-compliant shells you can achieve the same by
replacing "set -o noclobber" by "set -C" and
"set +o noclobber" by "set +C"; with other shells it may or
may not be possible, depending on availability of an
test-and-create -type atomic operation.
--
Tapani Tarvainen
> ...Mike
If the latter, why do you even need a file name? You can use the
standard C function tmpfile() in stdio.h to simply return a handle to a
new, temporary file, which will close and unlink itself upon program
exit. Check the following snippet:
#include <stdio.h>
int main(void) {
FILE *temp = tmpfile();
if (temp) {
/* Do things with temp */
}
return 0;
If the former, use the standard C function tmpnam(), also in stdio.h. ItQuote:}
/* File: GimmeTmpName.c */
#include <stdio.h>
int main(void) {
printf("%s", tmpnam(NULL));
return 0;
Then, when compiled, you can do things like this:Quote:}
$ ps -aef > `./GimmeTmpName`
Hope this helps,
Ryan.
...Mike
> > Thanks to all who answered. I finally settled on the date/time +
$RANDOM.
> That is not guaranteed to be unique.
> In particular, in a security-critical application it can be
> relatively easily predicted by a malicious attacker,
> and in a heavily-used machine two processes using it might
> accidentally hit on the same name ($RANDOM may well be same
> for each instance of the process, so if two of them run within
> same second...)
> That may or may not matter in your case (like, if you are creating the
> in a protected directory and you know there can't be two instances at
> the same time), but if you are using a public directory like /tmp and
> letting others run it, you might wish to do it more carefully.
> The multiple-instance problem can be handled simply by adding
> process ID ($$) to the string, but it won't help against
> malicious attackers.
> If your system has a working mktemp you could use it (but be
> warned, quite a few mktemp's are broken and vulnerable to
> attacks), otherwise here's a sample code that works with
> Korn shell:
> n=0
> set -o noclobber
> until MYTMP=/tmp/mytmp$$.$n; >$MYTMP ;do
> n=$(( n+1 ))
> done 2>/dev/null
> set +o noclobber
> Basically that keeps incrementing the suffix number until
> it finds a free filename, and then creates it in same atomic
> operation, so no other process can intervene.
> With POSIX-compliant shells you can achieve the same by
> replacing "set -o noclobber" by "set -C" and
> "set +o noclobber" by "set +C"; with other shells it may or
> may not be possible, depending on availability of an
> test-and-create -type atomic operation.
> --
> Tapani Tarvainen
1. How to get a unique filename?
How can I get a unique new filename for a temporary file?
I'm programming in C, and I want to create a temporary file in /tmp.
Is there any function that gives me a new filename?
Thanks.
2. QUESTION: firewall with snort creating dynamic rules.
3. unique filenames in Makefile
4. Things Linux would benefit from
5. automatically rename file to unique filename
7. Creation of unique filename for temp file.
8. LinuxPPC 1999 Q3 kernel version
9. How to generate a unique filename
10. generate unique filename in ksh on AIX
12. What is the official way to create unique filenames in Unix?
13. ensuring unique FTP upload filenames