Home Learn Linux Linux tmp Directory: Everything You Need to Know

Linux tmp Directory: Everything You Need to Know

by Nitish.S
Linux _tmp directory

Are you looking to expand your knowledge base on how temporary files are handled in Linux? Well, to help you out, here is a quick and comprehensive read on everything you need to know about the Linux /tmp directory.

What is the Linux /tmp Directory?

The /tmp directory in Linux based systems contains necessary files that are temporarily required by the system as well as other software and applications running on the machine.

tmp directory on Pop!_OS (example)

tmp directory on Pop!_OS (example)

For example, when you are writing a document, all the content inside that document is saved as a temporary file inside the /tmp directory. After you have saved it, it gets stored in your preferred location, and the temporary file gets removed once you exit the document.

What is the difference between the /tmp directory and the /var/tmp Directory?

Both /tmp and /var/tmp are used by programs as well as the system itself to store data temporarily. However, the key difference is for how long the data is stored inside these filesystems. The data retention period for /var/tmp is much more extended than that of the /tmp directory.

By default, all the files and data that gets stored in /var/tmp live for up to 30 days. Whereas in /tmp, the data gets automatically deleted after ten days.

Furthermore, any temporary files that are stored in the /tmp directory get removed immediately on system reboot. As such, the /var/tmp directory is used by programs to store data that needs to be preserved between reboots temporarily.

Can the Linux /tmp directory fill up?

The files and data that are stored inside the /tmp directory are only a few KB in size. It is doubtful, albeit possible, for the /tmp directory to fill up.

For an average system, there needs to be millions of temporary files stored in the /tmp directory to fill it up. This shouldn’t be something you should worry about if you run a single-user system that gets frequently shut down, as the rebooting system removes all the stored files in the /tmp directory.

However, if you are a system admin running a server that rarely gets rebooted and hosts thousands of users, then the /tmp directory filling up is a practical problem.

With more temporary files piling up in the directory, it is going to slowly eat up all the storage space, which is terrible news for the overall system health.

How to delete the files stored inside the /tmp directory?

Unless you know what you are doing, it isn’t recommended that you go around deleting the files inside the /tmp directory. These are important and required by the software running on your system. Removing them can potentially cause a system crash. Since the files and data stored in the /tmp directory get automatically deleted upon system reboot, it isn’t something you need to worry about actively.

However, as discussed earlier, for servers that run for months on end, it is necessary to clean up the temporary files from time to time before it floods the /tmp directory. To do this, we encourage you to take a more systematic approach instead of just deleting all the /tmp files by the bunch.

Here we have put together a detailed read on how to properly manage the Linux /tmp directory to help out system admins.

The correct way to delete Temporary Files and Manage the Linux /tmp directory

There are two things you need to consider when trying to manage the Linux /tmp directory. First, you need to know how to determine which files to remove from the /tmp directory, as randomly deleting the files will disrupt users’ activities.

Secondly, you need a way to automate the /tmp directory clean up process as it is going to be impossible to search and routinely delete the temporary files manually.

Considering these two points, the perfect solution would be periodically deploying user file housekeeping scripts using crontab. For example, let’s suppose that you want to routinely delete all user files stored in the /tmp directory that has not been used for the last three days.

Well, you can put this to action by first informing the users on your server about your new temporary file deletion policy, so that they can modify their usage activities accordingly. Next, you will need to write a script that will help you find all these files.

Here is an example that you can try:

find /tmp -type f \( ! -user root \) -atime +3

The above script will find all the /tmp files that haven’t been accessed in the last three days but will exclude all the files owned by the root user. To delete all these files, you will just to do a slight edit to the above script:

find /tmp -type f \( ! -user root \) -atime +3 -delete

To run this script periodically, you will need to copy the above text in a file and make it executable. For this tutorial, we will be creating the file tmp.cleanup.sh.

image-of-creating-a-crontab-script

Script for deleting /tmp files

Next, we will set up a crontab command that will execute the script every 6 hours. To do this, first enter the following command in the terminal:

$ crontab -e

This is going to open the crontab file in the editor. Next, you will need to copy and paste the given command in the editor and then save & exit.

0**/6** /opt/scripts/tmp.cleanup.sh
image-of-scheduling-a-crontab

Creating a /tmp file deletion crontab

This is going to run the script in /opt/scripts/tmp.cleanup.sh, every 6 hours, so you don’t have to worry about the /tmp directory getting flooded and eating up your system storage.

Of course, you are free to program the crontab to execute the script in any periodic interval you want. Here is an in-depth guide on how to create a crontab command to help you out.

I accidentally deleted the /tmp directory. Now what?

In the course of manually deleting the /tmp files, some users accidentally end up deleting the /tmp directory itself. Mess ups like this can and do happen. However, you can quickly restore the deleted /tmp directory using the discussed method.

All you need to do is enter the following commands in your terminal:

$ sudo mkdir /tmp
$ sudo chmod 1777 /tmp

The first line creates a new /tmp directory. The second line gives all users (owner, group, and other) access and permission to read, write, and run files in the /tmp directory.

Next, you can enter this command to check all the permissions of the /tmp directory to make sure all your users will be able to use it.

$ ls -ld /tmp

Once you have checked everything is a-okay, you will need to reboot your system. This will ensure that the programs start using the newly created /tmp directory.

Wrapping Up

So that covers everything you need to know about the Linux /tmp directory. However, if you still have some burning questions that we left out from this read, then feel free to ask them down in the comments section. We will happily resolve it to help you develop a better understanding and appreciation for Linux.

You may also like

3 comments

Glen March 27, 2021 - 12:38 PM

Is there a way to exclude files/directories under /tmp from being automatically purged by the system? I’m working with a router with very limited disk space and need to install a few programs. Looks like there’s plenty of room under /tmp the way the file system is partitioned, but nowhere else. Looking for a way to store permanent files/directories under /tmp.
Thanks.

Reply
Michael February 2, 2022 - 12:33 AM

Very helpful indeed, especially thanks for the crontab idea… this will work perfectly!

Reply
Kelvin March 5, 2022 - 2:11 PM

Great article, keep creating!

Reply

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.