12

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?

2
  • 1
    As a workaround, you might put some code into your shell's login files to "test & mkdir" as needed.
    – Jeff Schaller
    Apr 7, 2019 at 12:43
  • 3
    /tmp is likely a tmpfs filesystem. Those files aren't really deleted; they're simply stored in RAM and lost on a reboot. That's why you get answers that boil down to "re-create it on boot or login"
    – MSalters
    Apr 8, 2019 at 8:51

4 Answers 4

15

I use pam-tmpdir for this: it creates a user-private temporary directory at login. To set it up, add

session optional pam_tmpdir.so

to the appropriate PAM services; on a Debian-based system, installing the libpam-tmpdir package will offer to do this for you, or you can add the line to /etc/pam.d/common-session. The next time you log in, you’ll find a directory under /tmp/user with your user id, and TMP and TMPDIR set appropriately.

1
  • 7
    Could you explain what the appropriate PAM services are and how the line can be added to them? What files should be edited?
    – terdon
    Apr 7, 2019 at 13:53
4

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.

7
  • Will this a) create directories for all users and b) trigger from 'cold' startup as well?
    – ummw
    Apr 12, 2019 at 9:47
  • 1
    @Linux4win This answer addresses the needs of a single user, because this is how you phrased the question. It does not matter how the machine is booted (hot, cold, or lukewarm), but it would not trigger when resuming from hibernation (because that's not booting).
    – Kusalananda
    Apr 12, 2019 at 9:48
  • For some reason adding @reboot mkdir -p "/tmp/$USER" to the end of the crontab does not do anything, while running mkdir -p "/tmp/$USER" actually creates the directory. The second one does not work for me by the way, because I need to access my /tmp through other programs, and firing up the shell everytime is kinda annoying...
    – ummw
    Apr 12, 2019 at 10:00
  • @Linux4win The @reboot cron job does not create a directory? Are you getting any email sent to your account with any error messages from the cron daemon?
    – Kusalananda
    Apr 12, 2019 at 10:02
  • well no... Maybe because I haven't configured mail? Or is that not required?
    – ummw
    Apr 12, 2019 at 10:14
3

If you're running no a system with systemd and it uses systemd-tmpfiles to manage the cleanup, then you should configure the directory using that system.

Here's a full documentation. You can likely achieve what you want by creating /etc/tmpfiles.d/something.conf with contents like:

d     /tmp/your_username   0750 your_user your_group  - -
1

As pointed out in the comments, /tmp is for files that are transient and won't be missed if they're deleted. If you're in the middle of something and the machine reboots, anything you've written there will disappear and you'll have to start over, which means those files might not have been so temporary after all.

I maintained a temporary directory in my home directory for many years starting when a gigabyte was a lot of space and found that it required periodic grooming to get rid of files that had become old and crufty. Most of the time what I had left after that process was less than a couple of weeks old, so I now clean it up with a cron job that runs once daily:

find $HOME/tmp -depth -mtime +30 -print0 | xargs -0 -r rm -rf

Anything I download or create as semi-temporary goes there and the system cleans up anything I leave behind. Anything that needs a permanent home gets one long before the cron job gets rid of it.

2
  • mkdir a a/b; touch a/b/c. Now wait until your 30 days has expired. As you delete c it'll update b, which will mean it has to wait for a further 30 days. Likewise with a when you delete b. Secondly, if you create files in b but don't touch a for 30 days, the a directory will get hit with rm -rf and wipe out your recent files under b. Apr 7, 2019 at 18:31
  • find "$HOME"/tmp -mtime +30 -type f -delete; find "$HOME/tmp" -depth -mmin +60 -type d -exec rmdir {} + 2>/dev/null works for me Apr 7, 2019 at 18:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .