how to clear temp files in ubuntu

How to Clear Temp Files in Ubuntu

Every operating system uses a temp folder for storing temporary files required to runs its various processes and applications. Ubuntu also has a temp folder that stores temporary files, whose contents are cleared on every system reboot. But if you are running a production server that does not reboot often then this temp folder will grow in size over time and may cause problems. So it is advisable to regularly clear temp files in Ubuntu. In this article, we will learn how to clear temp files in Ubuntu.


How to Clear Temp Files in Ubuntu

By default, Ubuntu stores all temporary files in /tmp folder. There are several ways to delete temp files in Ubuntu. We will look at them one by one.


1. Using find

Find command is pre-built in every Linux distribution and does not require any installation or setup. It allows you to find files & folders that satisfy specified condition. It also allows you to run other commands such as rm, on the files & folders listed in its result.

Open terminal and run the following command to find and delete all temp files in /tmp.

$ sudo find /tmp -type f -delete

In the above command, find command will find all files in /tmp folder. The -delete option is used to delete the searched files. However, the folder will remain.

If you want to delete the folder also, then run the following command.

$ sudo find /tmp -exec rm -rf {} +

In this case, find command will find all files in /tmp and then run rm command on them.


2. Using rm command

You can also use rm command to delete all files in /tmp folder. First we go to /tmp folder using cd command.

$ cd /tmp

Next, use rm command to delete all its contents.

$ sudo rm -rf *

If you want to delete the /tmp folder itself then run the following command from outside /tmp folder.

$ sudo rm -rf /tmp


3. Create Shell Script

If you want to automate the deletion of temp folder, you can create a simple script for the same. Open terminal and run the following command to create an empty shell script.

$ sudo vi clear_temp.sh

Add the following lines to it.

#!/bin/sh

sudo find /tmp -type f -delete

Save and close the file. Run the following command to make it executable.

$ sudo chmod +x clear_temp.sh

Now you can run the above shell script whenever you want to clear temp folder.

$ ./clear_temp.sh


4. Using Cronjob

It is advisable to regularly delete your temp folder. For this purpose, you can setup a cronjob to run the above command or shell script. Open terminal and run the following command to open cronjobs in your system.

$ crontab -e

Add the following line to it to run the above find command (section #1) to automatically delete temp files every day at 10.a.m.

0 10 * * * sudo find /tmp -type f -delete >/dev/null 2>&1

Save and close the file. You can modify the above command as per your requirement. If you want to run the above shell script instead of command you can add the following line in your crontab.

0 10 * * * sudo /home/ubuntu/clear_temp.sh >/dev/null 2>&1

In the above case, remember to mention the full path to your shell script (e.g. /home/ubuntu/clear_temp.sh) and not relative path(./clear_temp.sh).


Check If Temp Folder is Full

Here are some commands to check if the /tmp folder is full or not.

$ sudo df /tmp

The above command will give you information about the amount of space occupied by /tmp folder. If you want the disk space information in human readable format such as MB, GB, etc. use the -h option in above command.

$ sudo df -h /tmp

If you just want to know how much disk space is used by /tmp folder run the following command.

$ sudo du -sh /tmp

If you want to get a detailed tree-like information about the various files & folders in your /tmp folder, run the tree command.

$ sudo tree /tmp

You can also use tree command with du command to list the amount of data in the various files & folders in /tmp folder.

$ sudo tree --du -h /tmp

In this article, we have learnt how to check if /tmp folder is occupying a lot of space and also how to delete it regularly. We have also learnt how to create cronjob and shell script to regularly clear temp folder.

Also read:

Git Pull vs Git Fetch
How to Kill User Session in Linux
Git Tags vs Branches
How to Sleep Function in Python
How to Fix Access Denied for Root User in MySQL in Ubuntu

Leave a Reply

Your email address will not be published. Required fields are marked *