
On 17 April 2018 at 11:43, D. Hugh Redelmeier via talk <talk@gtalug.org> wrote:
I often want temp files.
Putting them in /tmp means that they will automatically be discarded on reboot -- kind of handy. But it means that they are public: they are in a space shared with all other users (if you have other users). I consider it a bad habit for that reason.
Lots of people put temp files in the current directory or in their home directory. That's what I do. But then, when you come back a week later, how do you know that this file is garbage? The best way is to have a naming convention.
Many people use tmp, tmp2, tmp3 as names. I find those visually too like real filenames and longer to type than I want. The names I use are 0, 1, 2, 3. They are short, distinctive, and unreasonable as permanent filenames.
You can find the litter you left behind with: locate -r '/[0-9]$' You will notice that the system does have some files that match too so you cannot blindly delete all matches.
To me, it is obvious. But I didn't think of it. I copied this from Chris Sturgess almost 30 years ago.
If ID-ing the source of the files is an issue (as it would be if they aren't occasionally auto-deleted, such as in /tmp/), I often use mktemp (as has already been suggested) in combination with part of the filename being '$(basename $0)' so you know what script created it. That's a Bashism - there's probably an equivalent in other shells. -- Giles https://www.gilesorr.com/ gilesorr@gmail.com