A smart way to use /tmp

I always loved use tmp directory to manage my “temporary files”. First of all, which files can be considered as “temporary”, and what does temporary exactly means? Well.. In my opinion, we can address as temporary all those files used only during a single Session. Such as:

  • files downloaded from browser – typically “to take a look or a single read”, they will never be opened again.
  • “text_files / notes / drafts / patches / etc..” – bound to be sent by email (and never used again).
  • “apt-get source” – do you use it? 🙂
  • many more … (pictures, samples, songs, backgrounds, code_branches, videos…)

If you use the standard $HOME/Downloads (or the Desktop, or Home or whatever_you_use) directory to locate all those files, you already know which kind of mess will be there in few days, unless you do not spend part of your time to delete those files no longer used. Looking for a solution to this I figured out that set /tmp directory (not really, but think about that) as my Download directory can be a good choice… It will be cleaned to each boot, so I can take no interest about files I download during a Session – and store only those I consider “important”, by moving to my “Downloads” (or “Documents”) directory. It can work, no? No. In fact that’s not all. I also love mount /tmp in Ram in order to speed up a bit the system… And place my downloads in /tmp can be dangerous for the system –  sometimes downloaded files can be bigger than we expect (try to think to a 20MB pdf). So, one of the best way (in my opinion) to have a clean “downloads” directory is create a local ~/tmp directory and use it as the default download directory for my Desktop and in the following lines I will show you how set it up.

Let’s start with mount /tmp in Ram, edit your /etc/fstab appending the lines below:

# RAM Disks
tmpfs /tmp tmpfs defaults 0 0

Next step, create a ~/tmp directory (or wherever you prefer) and set it as Downloads directory:

mkdir ~/tmp
xdg-user-dirs-update --set DOWNLOAD ~/tmp

Now create a script able to clean the directory to each login. I choose a file named ~/.initrc (you can use it to launch other applications, like conky). Use your preferred editor and insert the following lines:

# Clean tmp dir:
find $HOME/tmp -mindepth 1 -delete &

Save the file and link it into yourStartup Applications” (search it through Unity), I use the following:

That’s all. My simple way to have a clean Home directory.

10 thoughts on “A smart way to use /tmp

  1. bochecha

    I agree that ~/Downloads can get messy real quick.

    But now, instead of a messy ~/Downloads directory with all the random stuff you’ve downloaded, you have a messy ~/tmp directory.

    What’s the point?

    The whole idea of ~/Downloads is that it will contain all that stuff we download and don’t care about, because if we did, we’d move it to ~/Documents, ~/Pictures, etc…

    So why not just set your script to cleanup ~/Downloads and remove one level of indirection?

    1. l3on Post author

      Because I also use ~/Downloads for other stuff, for instance to locate my torrents and podcast, files that I want to preserve 😉

      You don’t?

      1. bochecha

        No, when I want to preserve something it goes to a proper folder.

        I use ~/Downloads exactly the way you use ~/tmp

  2. gaspa

    Now everyone knows how to send your PC out-of-mem! 🙂
    «Ehy, take this file .dsc»
    … from now on, don’t trust me. LOL

      1. gaspa

        uhm, ok, then I can’t catch the point. /tmp currently isn’t mounted as tmpfs.
        Why you need to put it in ram?

        If I understood correctly now, the only important bits of all your needs is the cleaning script. Or am I still wrong?

        1. weboide

          I’m with you Gaspa. The OP talks about setting up /tmp as tmpfs, but then he simply creates ~/tmp without any relation to /tmp…

          1. l3on Post author

            You’re right… The point (maybe I miss there) is: I always used /tmp to download my files…
            But, since one day I decided to it put in RAM, I have been forced to create a local tmp dir (~/tmp) and make a script able to delete the content during login (in the same way /tmp gets clean at boot).

            Ah – The post is called ‘A smart way to use /tmp’ – that’s the relation you’re looking for. 🙂

Leave a Reply