6

What's an effective way of creating a new /tmp directory, that behaves just like /tmp (purged on reboot, etc.), but in a different location? In my case, I want to create a ~/dev/tmp directory where I want to do quick tests, and I want that directory not to bloat and to be automatically emptied.

I'm looking for something that could be the most standard and could potentially work in a variety of distributions.

Thanks

1
  • 1
    The emerging standard is pam-tmpdir, but the current implementation doesn't automatically clean the dir upon reboot. Most common use is to safely create a mode 700 /tmp/username directory upon login, and set TMP and TMPDIR in the environment. Jul 29, 2014 at 16:53

2 Answers 2

5

Using the @reboot cron keyword, this will execute the specified command once after the machine got booted every time.

@reboot rm -rf /dev/tmp/*
1
  • Consider having the cron entry rename it to a temporary name and create a blank new directory. Then, remove the old renamed directory in the background (or as a separate cron job). This way a new, blank directory is immediately available, and removing the old directory's contents won't block anything.
    – user117529
    Nov 26, 2018 at 3:11
5

An easy way is to just create a directory in /tmp and use a symlink:

mkdir /tmp/mine
ln -s /tmp/mine /home/me/tmp

You may want to chmod 700 /tmp/mine to keep it private.

If you instead want to mount an actual separate tmpfs directory:

mount -t tmpfs -o size=100M tmpfs /home/me/tmp

You need root privileges to do this, but normal permissions rules apply after that. Make sure you specify a size since:

The default is half of your physical RAM without swap. If you oversize your tmpfs instances the machine will deadlock since the OOM handler will not be able to free that memory. https://www.kernel.org/doc/Documentation/filesystems/tmpfs.txt

These can be umount'd normally, at which point all the information is lost.

5
  • Wouldn't the /tmp/mine folder be deleted on reboot and leave and dangling symlink?
    – Holloway
    Aug 20, 2014 at 16:15
  • @Trengot Yes, but dangling symlinks are not defunct. When/if the target is recreated, it won't be dangling anymore. I.e., you do need to automate creating the directory in /tmp at boot (or login) if you want it available by default. The point of the symlink is just to fulfill the "different location" criteria.
    – goldilocks
    Aug 21, 2014 at 16:08
  • true, but if you're going to recreate the folder in tmp everyday, you might as well delete the contents of the the new tmp-style folder.
    – Holloway
    Aug 22, 2014 at 8:30
  • @Trengot It's explicit in the question that the OP is asking WRT systems that use tmpfs ("purged on reboot, etc."). That's the norm now-a-days, I think. So there is no need to arrange deleting the contents, that will happen by nature, as does the creation of /tmp during boot. However, if you want a subdirectory there automatically by default, you do need to arrange that (i.e., a mkdir later in the boot process). As long as the symlink exists, nothing else has to be done...
    – goldilocks
    Aug 23, 2014 at 13:01
  • ...The suggested alternative (creating a separate tmpfs directory w/ mount) does not require its contents be deleted either, since it by nature cannot persist across a (re)boot. The mount would replace the mkdir in the boot process.
    – goldilocks
    Aug 23, 2014 at 13:05

You must log in to answer this question.

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