Linux tmp Directory: Everything You Need to Know

If you are coming from the Windows environment, the chances are that you may think the tmp directory is something safe to delete. That's not the case! Unless you know what you are doing, we don't want advice to remove anything in the tmp directory. Here is everything you need to know. Read on.

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.

Nitish.S
Nitish is a Technical Writer with five years of experience. He enjoys covering new tech and has a special love for Linux. He also has a keen interest in Blockchain and WordPress.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,414FansLike
377FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

Mastering the SQLite Database in Python

SQLite is a relational database management system based on the SQL language; it is a serverless, Zero-configuration database engine. It is one of the...

Basics of Working with the SQLite Database in Python

A database is one of the most useful and popular files for storing data; they can be used to store any kind of data, including text, numbers, images, binary data, files, etc. SQLite is a relational database management system based on the SQL language. It is a C library, and it provides an API to work with other programming languages, including Python. It does not require a separate server process to be run as needed in large database engines like MySQL and Postgresql.

5 Ways to Check the Linux Version

When most people talk of Linux, they are always referring to a Linux distribution. However, this is not the case. Linux itself is a kernel which acts as a bridge between user applications and the hardware. When we talk of a Linux distribution, we refer to an operating system developed from the Linux kernel. A distribution comes with a package manager, pre-installed applications, a Desktop Environment, and several more features.

Getting Started with Linux Operating System

The Linux operating system brings forth a vibrant mix of features and security, making it the best alternative to macOS or Windows operating systems. In this post, we will give you a master guide on Getting started with Linux systems - taking you from a complete beginner to a level where you can begin testing the various Linux distributions available with much ease.

How to Create a Comprehensive Mail Server on Ubuntu

Postal is a free and open-source mail server used to send and receive emails. It comes loaded with tons of excellent features and functionalities, making it extremely popular among large organizations as well as in enterprise settings.

The 10 Best Linux Performance Monitoring Tools

Do you want to monitor the performance of your Linux system? Are you looking for some powerful performance monitoring tools to help you out? If you agree, it's your day as we have put together a detailed list of the ten best Linux performance monitoring tools.

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

6 ways to find out your Linux file system type

Any Operating system in the market whether its Windows, Linux, Unix, macOS, and any other, must be able to access and manage files and data on storage devices.

10 Best Screen Capturing Software for Linux

Do you want to make a video tutorial for YouTube or show-off your epic victory royale to your friends? Look no further — as we have compiled the 10 Best Screen Capturing Software for Linux. Whether you need to record your desktop screen or take a simple screenshot, FOSSLinux has got your back.

VIDEO: MX Linux 19 Features and Desktop Tour

MX Linux is a Linux distribution based on Debian stable and using core antiX components. The distributions MX Tools is very popular among users and combined with other several ready-to-use tools, it is great for users who prefer to tweak their distro to their liking. In this video, we will take you through the features tour showing casing MX Linux 19.

What is FOSS, and how does it differ from Freeware

The rise of the Linux operating system, in all its various distributions, over the past few decades has catapulted the popularity of Free or Open Source Software (FOSS). Let's guide you in understanding what is FOSS, how it differs from freeware and is Linux a FOSS.