Unique Names for Temporary Files

All users share /tmp (), 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 () (in this case, ).

If you use a subshell (), 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 (). 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 () 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 () $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 (). 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 , , 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 () (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 tutorial.
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 ().

- JP