Everything Essential About the tmp Directory in Linux
Learn some crucial things about the /tmp directory. You'll also learn how it is different from the /var/tmp directory.
If you have been using Linux for a while, you must have come across the /tmp
directory.
You may have some idea about it but you probably didn't pay enough attention to it.
Then there is also a /var/tmp directory that sounds similar.
So in this article, I will walk you through some crucial things that you need to know about the /tmp
directory. I'll also discuss how it is different from the /var/tmp directory.
What is the /tmp directory in Linux?
As the name suggests, the tmp (temporary) directory under root is used to store the data used by the system and user applications to store the data that are needed for a short period of time. Most Linux distributions are preconfigured to empty the tmp directory after each reboot.
Sounds complex? Let me give you an example.
So let's suppose you are installing software in your system so the installer may store some files that are needed during the installation.
Similarly, while working on a project, your system may store the files in the tmp directory when making changes or they can also be the auto-saved versions of that file.
In simple words, the tmp directory is nothing but a directory used to store the files that are needed temporarily and can be removed once they are no longer needed.
Are /tmp and /var/tmp the same? No!
Yes, there is a significant difference between the /tmp
and the /var/tmp
directory.
The short answer is how they both deal with the temporary files.
The /tmp
directory is used to store the short-lived temporary files whereas the /var/tmp
directory is used to store long-lived temporary files.
Want more details? Here you have it!
- Endurance: Generally, the files stored in the
/tmp
directory are removed at the boot time whereas the files inside/var/tmp
are kept even after reboot. - For user VS Systemwide: Typically, the files inside the
/tmp
directory can be accessed by every user whereas the files of/var/tmp
are mostly user-specific. - Usage (the most crucial difference): The
/tmp
directory is used to store the files that are needed for a short time like for the installation of a package. Whereas the/var/tmp
directory is used for files that are needed for a longer period of time like system backup or log files.
Automate tmp directory cleaning
As I said earlier, most, if not all, distributions clean the /tmp directory when you reboot your Linux system.
If that's the case, then why do you need to explicitly clean the /tmp directory? Because you don't reboot your server everyday like a desktop computer. I mean check the uptime of your server; it might be running for weeks, if not for months and years.
To automate the cleaning of the tmp directory, the most critical thing is to identify what to remove in the first place.
So the sweet spot is to remove the files that are not used for the last three days and are not owned by the root.
And for that purpose, you can use the find command in the following manner:
sudo find /tmp -type f \( ! -user root \) -atime +3 -delete
But this won't automate the process.
For that, you'd have to create a cron job to automate the execution.
First, open the root crontab using the following:
sudo crontab -e
If you are using the cron table for the first time, it will ask you to choose your preferred text editor. I will recommend using the nano:
Once done, go to the end of the file in nano using Alt + /
and paste the following line into the file:
0 0 * * * sudo find /tmp -type f ! -user root -atime +3 -delete
Save changes and that's it!
Did you know about the black hole of Linux filesystem?
I'm talking about the /dev/null
directory here as whatever is sent there, can not be traced back! Want to know how it can be used? Here you have a detailed guide:
I hope you will find this guide helpful. And if you have any questions or suggestions, leave a comment.
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.