The “/tmp” or tmp directory (temporary directory) in Linux is a place where any program stores the necessary files it can use during an executive session. It is one of the most used directories in Linux. Let us say you are writing a document. You are doing that in vscode. The vscode will save a temporary copy of your document in the tmp file and will update it on a regular basis. You can always go there and can restore it if necessary.
Learning about the tmp directory thoroughly is a must if you want to be an effective Linux user. Knowing how it works, what are the attributes of this directory, etc., is going to enhance your Linux controlling a lot. We shall answer all of the possible questions that can come to your mind. Stay with us to learn.
Linux tmp Directory: What is the Purpose?
The /tmp directory contains all the required files that are going to be required “temporarily” during program execution. In case of any crash or unexpected event, you can retrieve your file from the directory. Also, you can see which sub-programs are being used during the execution of a program.
When you save your file at the desired destination and exit the program properly, all the temporary programs or documents become vanished leaving the directory empty. If it is not emptied, then after a reboot of your PC, it will become empty.
Can This Directory Be Filled Up?
For a normal Linux user, it is really hard to fill the tmp directory up. The files that are stored in the tmp directory are really small, some kilobytes in size. So you have to have a gigantic amount of files gathered there to fill the directory. It is not impossible but is really hard.
Usually, we run one or two users on our PC and frequently turn our PC off. Using a lesser number of users creates a lesser amount of temporary files. And shutting a PC down erases all the temporary files—no need to worry.
If you are running a server containing tons of users, then the tmp directory is a headache. You can not shut a server down frequently, so there is no chance for automatic emptying. You have to delete the temporary files manually for this scenario.
Emptying the tmp Directory Mannually
As an average Linux user, it is not recommended to empty this tmp directory manually. That will lead to a system crash because the files stored inside it are necessary to run the programs that are opened right now.
If you are running a server, then there is a systematic approach to emptying the tmp directory. In the proceeding sections, we are going to discuss that approach in great detail.
To delete files from the tmp directory effectively, you have to have crontab in your system. If you do not have any idea about the crontab, then let me brief that in short.
The crontab is an application that does a certain pre-scheduled operation in a Linux system. For example, let us think that you have to update your system at a regular periodic interval.
The crontab allows you to accomplish this task. You just have to tell it when the operation is going to take place and how it should be done. At that particular instant, crontab will do the job according to your instructions.
In a server, there are tons of users, and each of them has its own temp files. Randomly deleting them will cause severe interference in the user’s activity. You have to search for those temp files that are out of use and then have to delete them. It’s a pretty tedious and monotonous thing to do. Also, it will kill your time.
A feasible solution is to delete those files automatically after a certain period that is not accessed in that period of time. This automated process is efficient and time-saving. Also, it saves your labor.
To execute these automated operations, you need to take help from the crontab. That’s why I had discussed it just a while ago. Let us say you want to delete the unused files from the tmp directory once a week.
First, notify the users of your server about this. Tell them that you are going to delete the temp files that are not accessed in a week. Executing the operation without announcing the policy may lead to a massacre.
Procedure to Cleaning the Directory
Then you have to type a script that is going to find the desired files. The command is as follows:
find /tmp -type f \( ! -user root \) -atime +7
After executing it, you will be able to find all the files (except the files of the root user) that are not accessed within the last seven days. Now you are half done. You have found those unnecessary files that are to be deleted. To delete them, you have to execute this:
find /tmp -type f \( ! -user root \) -atime +7 -delete
So, we have found a way to empty the tmp directory in Linux in a systematic manner. But the owner of the server has to execute the script manually every week. This can be avoided by using the crontab. To make the script periodically executable, let us make a file containing the syntax. Now we are making a file named “tmp.swipe.sh” (quotation signs are excluded).
Then a crontab command is to be set. First, execute this:
It will open the crontab file. Then execute this command there. It will run our script every Monday at 12 pm.
0 0 * * mon /opt/scripts/tmp.swipe.sh
After writing this, save an exit.
So, we have created a way to clean the tmp directory once a week. You do not have to think about it anymore. Your time, space, and labor are saved. You can also change the time interval according to your convenience. Here we have used the seven-day period only as an example to demonstrate the procedure.
There Is Directory Named /var/tmp. What Is the Difference Between this and /tmp?
Both of them basically play the same role in an operating system. The only difference is the deleting period.
If we reboot our system, the files in the /tmp are deleted automatically, while the files staying in /var/temp are not deleted only by a system reboot.
Also, the files inside /tmp are being emptied automatically after ten days. On the contrary, the files inside /var/tmp is deleted once a month.
What If I Delete the tmp Directory Accidentally?
Restoring the tmp directory is often needed because many Linux users delete the whole directory by accident. The restoring process is not a tough task at all. Execute the following commands stated below:
sudo mkdir /tmp
sudo chmod 1777 /tmp
The “mkdir” creates a new tmp directory. And then, we are giving the users access to the directory. Do a double-check about the permissions by executing this:
ls -ld /tmp
Now shut your PC down. And turn it on again. Your new tmp directory is ready for being used. You are all done.
We have discussed a lot about the tmp directory in Linux here. Keeping these points in mind will be sufficient for any user to run a Linux system with ease.
Though, in my opinion, this will suffice, maybe you have something in your mind that should be covered in this post, but I have not done that. To man is err. Leave a comment with that necessary part that was supposed to be mentioned here. That is all for today.