UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 21.2 A Better Place for Temporary Files: /tmp Chapter 21
More About Managing Files
Next: 21.4 Why Both /tmp and /usr/tmp?
 

21.3 Unique Names for Temporary Files

All users share /tmp (21.2), so you should make unique filenames there. The best way to do this is by putting $$ in the filename. For example:

% vi /tmp/jerry.$$
"/tmp/jerry.12345" [New file]
% lpr /tmp/jerry.$$
% rm /tmp/jerry.$$

The shell replaces $$ with the shell's PID number (38.3) (in this case, 12345).

If you use a subshell (38.4), or have more than one login session or window, and want to share the same temp file, $$ won't work for you. In that case, just pick a unique name. You could use today's date instead.

To give yourself both options with a minimum of work, here are lines for your shell setup files (2.2). The left column has lines for csh-like shells, and the right is for sh-like shells.





`...` 
[n] 
.cshrc:		.profile:

set tf=/tmp/jp$$	tf=/tmp/jp$$

.login:		export TF
set date = (`date`)	set `date`
setenv TF /tmp/jp$date[4]	TF=/tmp/jp$4

(The last two lines grab the fourth word - the current time - from the output of the date (51.10) command.) When I want a temporary file in my current shell, I type:


* 
% grep foo bar > $tf-1
% grep wheeze bar > $tf-2
% more $tf-*

The shell expands the shell variable (6.8) $tf-1 into a filename like /tmp/jp2345-1, and $tf-* expands into all my temporary files in this shell. Usually, that's great. But if I go to a subshell, do a shell escape, and so on, the temporary files I make with $tf won't be the same as the ones I make in my login shell because the PIDs are different. If I need them to be the same, I use $TF, the environment variable (6.1). It's set to the time I logged in. And because environment variables are passed to child shells, the name (like /tmp/jp09:34:56) will be the same in subshells:




[..] 
% someprog > $TF-1
...
% otherprog > $TF-6
% sh
$ head $TF-[16]

If I'll be using a file for more than a minute or two, I might forget what's in which file. So I leave myself a note in shell variables named xfn and environment variables named XFn-where "xf" means "explain file" and n is 1, 2, etc. to correspond to the variable. If I don't remember which have what, I get a list by piping the output of set (for shell variables) or printenv or env (6.1) (for environment variables) through grep. For example:

% sort -t: +2 $tf-2 > $tf-3
% set xf3='sorted list of chapter 21 files'
   ...later...
% set | grep xf
xf1     sorted list of chapter 20 files
xf3     sorted list of chapter 21 files
% lpr $tf-3

csh_logout
sh_logout
To clean up when I log out, I added the lines that follow to the C shell .logout file. The Bourne shell version is similar, but it needs a couple of tricks to work on some shells; it's on the CD-ROM.


nonomatch 
-d |& 




$< =~ 
# CLEAN FILES (IF ANY) OUT OF /tmp:
set nonomatch
set tmpf="\`ls -d $tf-* $TF-* |& grep -v ' not found'\`"
if ( "$tmpf" =~ ?* ) then
    echo; echo "Your files in /tmp:"
    ls -d $tmpf
    echo -n "'rm -rf' them? [ny](n) "
    if ( "$<" =~ y* ) rm -rf $tmpf
endif

If I made any temporary files from my login shell or any subshells, I get this message when I log out:

% logout

Your files in /tmp:
/tmp/jp2345-1   /tmp/jp2345-2  /tmp/jp2748-1  /tmp/09:23:45-1
'rm -rf' them? y

Another way to do this is with a script like del (23.6).

- JP


Previous: 21.2 A Better Place for Temporary Files: /tmp UNIX Power ToolsNext: 21.4 Why Both /tmp and /usr/tmp?
21.2 A Better Place for Temporary Files: /tmp Book Index21.4 Why Both /tmp and /usr/tmp?

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System


Banner.Novgorod.Ru