Prevent Directory in /tmp from Being Deleted

tmp

I often use the /tmp directory on my Linux machine for storing temporary files (e.g. PDFs from a site that wants me to download it first etc.) and I often create a directory with my username. But at every startup it (including all files) gets deleted. Now I know I can put it in /var/tmp, but I want all its contents to be deleted, but for the directory itself to be kept.
So:

tmp
 |- me # this should stay
 |  |- foo1 # this should be deleted...
 |  |- bar1 # ...and this as well
 |- other stuff...

Is there any way to do this? Maybe with permissions or with a special configuration?

Best Answer

One solution would be to use a @reboot cron job:

@reboot mkdir -p "/tmp/$USER"

Adding this to your crontab with crontab -e would make it execute whenever the machine boots up.

Or, use

mkdir -p "/tmp/$USER"

in your shell's startup file.

In either case, you may also want to use

TMPDIR=/tmp/$USER
export TMPDIR

in your shell's startup file if you want to use that directory as the default temporary directory.

Related Question