Create new temporary directory

10,903

Solution 1

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

@reboot rm -rf /dev/tmp/*

Solution 2

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.

Share:
10,903

Related videos on Youtube

Alain Jacomet Forte
Author by

Alain Jacomet Forte

Updated on September 18, 2022

Comments

  • Alain Jacomet Forte
    Alain Jacomet Forte over 1 year

    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

    • Admin
      Admin almost 10 years
      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.
  • Holloway
    Holloway over 9 years
    Wouldn't the /tmp/mine folder be deleted on reboot and leave and dangling symlink?
  • goldilocks
    goldilocks over 9 years
    @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.
  • Holloway
    Holloway over 9 years
    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.
  • goldilocks
    goldilocks over 9 years
    @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
    goldilocks over 9 years
    ...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.
  • user117529
    user117529 over 5 years
    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.