Ubuntu: How do I prevent a folder inside of /tmp from being cleaned up?


This question is about Ubuntu 14.10 on my developer laptop.

I've got a folder in /tmp that is used by an application to put temporary stuff in there. This application usually makes a temporary folder in my homedir and deletes it afterwards. For some reason that doesn't work when the homedir is encrypted. So instead, I made a symlink to /tmp/foo inside my homedir. My application can write there and make it's temporary subfolder.

Now /tmp/foo gets deleted every time I reboot my machine. Until now I've just recreated the folder manually after reboot. Now I learned in How is the /tmp directory cleaned up? that there is a job doing that.

I've looked at /etc/init/mounted-tmp.conf but my bashfu and especially my findfu are not sufficient to do what I want. Here's an excerpt from that file:

   EXCEPT='! -name .              ! ( -path ./lost+found -uid 0 )              ! ( -path ./quota.user -uid 0 )              ! ( -path ./aquota.user -uid 0 )              ! ( -path ./quota.group -uid 0 )              ! ( -path ./aquota.group -uid 0 )              ! ( -path ./.journal -uid 0 )              ! ( -path ./.clean -uid 0 )              ! ( -path "./...security*" -uid 0 )'       # Remove all old files, then all empty directories     find . -depth -xdev $TEXPR $EXCEPT ! -type d -delete     find . -depth -xdev $DEXPR $EXCEPT -type d -empty -delete  

What I want to do is add a condition that makes it delete everyting inside /tmp/foo, but not /tmp/foo itself. How do I do that?


/etc/init/mounted-tmp.conf is part of the mountall package, so any updates on that package and the suggested changes will be reverted.

$ sudo dpkg -S /etc/init/mounted-tmp.conf   mountall: /etc/init/mounted-tmp.conf  

Instead, according to the Filesystem Hierachy standard (FHS);

Regarding /tmp:

Programs must not assume that any files or directories in /tmp are preserved between invocations of the program.

Regarding /var/tmp:

The /var/tmp directory is made available for programs that require temporary files or directories that are preserved between system reboots. Therefore, data stored in /var/tmp is more persistent than data in /tmp.

So you should change your symbolic link to use /var/tmp instead of /tmp.


Not strictly an answer to your question, but you might find /var/tmp to be a more suitable location, as it doesn't get cleaned up over a reboot. It's designed for temporary files that should not be automatically discarded after a short time.

What I often do, however, is create myself a folder under /opt to store random things I don't want in home. That's a suitable place to put things that are outside the main OS's control.


Like so:

EXCEPT='! -name .              ...              ! ( -path "./foo" )'       # Remove all old files, then all empty directories     ...     find /tmp/foo/* -depth -xdev $TEXPR -delete  


$ cd /tmp/foo/  $ mkdir 1 2 3  $ touch 3 4 5  $ find /tmp/foo/* -depth -xdev $TEXPR -delete  $ ls /tmp/foo/  $  

I do agree with user aap: You either should take care of this in the software used by re-creating the directory there if they are tmp files or use another directory if they are not tmp files that is not purged.


It is a bad idea to have files with future value, write to the /tmp directory (/var/tmp, as suggested by others, is a better place). That said, you might want to give a shot at chattr. This should be run after the application exits, but before a shutdown. Remember that this operation won't let anything write to that directory henceforth.

touch /tmp/foo/ddmmyy/.001_immute_me chattr +i /tmp/foo/ddmmyy/.001_immute_me


Run this:

echo "if [ ! -e /etc/foo/ ]; then mkdir /etc/foo/; fi; rm -rf /etc/foo/*" > /bin/foodel; sudo chmod 755 /bin/foodel   

then Add this to /etc/inittab see here: where is inittab file?:


That will create /etc/foo on boot if it doesn't exist, and then empty it.

This will do the same on bash login:

echo "/bin/foodel" >> ~/.bashrc  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »