How to clean temporary files on Ubuntu

Endrit Qerreti

Endrit Qerreti

Every system generates temporary files automatically once you open an application or do something on your computer. These files are required by the system so you can run your applications faster.

When you launch an application, a temporary file of that application gets created on the folder where these files are stored. Temporary files on Ubuntu are stored on tmp directory, this directory contains all the temporary files that your system has generated automatically.

Over time this folder can take a lot of space if you don't clean it.

There are several ways to clean temporary files, and one of them is by rebooting your machine.

These files gets deleted automatically when you reboot your machine. However, when it comes to servers, rebooting it's not always an option as no one wants to reboot a production server.

In this tutorial, you will learn how to delete the temporary files on Ubuntu

1) Clean Temporary files using rm command

Temp files are located on /tmp , so to clean all temporary files inside the tmp directory, you can use the rm -rf option

sudo rm -rf /tmp/*

💡
Note: Root permission is required in order to be able to delete everything in the tmp directory.

2) Clean temporary files using find command

Find command allows you to search for any file or directory on your system.

To give you an example how find command works , Let's say you put some images somewhere but you can't find them.

Instead of searching for these images via file manager, you can use find command as find command allows you to specify the file extension of files that you are looking for.

Now when it comes to cleaning temporary files with find command, we know where temporary files are located, so we don't need to search for something in this case, but delete those files instead.

So to clean temporary files using find command you can use the -delete option

sudo find /tmp -type f -delete

💡
Note: The command above will delete everything in the tmp directory.

You can also use the mtime option.

This option allows you to delete files that are older than x period of time.

Let's say you want to delete temporary files that are older than 7 days, to do so run the command below

sudo find /tmp -type f -mtime +7 -delete

3) Clean temporary files with Cronjob

Instead of cleaning temporary files manually, you can use cron and bash to automate this process. Doing this would allow you to clean those files automatically without having to login on your server to delete those files.

First, create a file called clean_temp.sh on your system. You can use the touch command to create a file

touch clean_temp.sh

Next, open clean_temp.sh using nano text editor

sudo nano clean_temp.sh

And copy the bash script below into clean_temp.sh

#!/bin/bash

sudo rm -rf /tmp/*

The bash script above will execute sudo rm -rf /tmp/* when you launch it, it is a very simple script as we only need the script to delete temporary files.

Next, you need to make the bash script executable. To do so, run the command below on the same directory where you created the script.

By default the bash script will be created on /home directory.

chmod +x clean_temp.sh

Now once you make clean_temp.sh executable, you can test it to see if it runs correctly.

./clean_temp.sh

This command will run the script that you created, but it won't show anything as output. To verify that the script cleaned all temp files, you can manually view the content of /tmp directory by using the ls command

ls /tmp

If everything is cleaned, then you shouldn't see anything listed on your terminal.

Now that the script runs correctly, you can proceed to setup the cron job. To create a cronjob is very easy and it can be done by using the crontab -e option.

crontab -e 

Once you run the command above, you will be taken to crontab file. This is the file that contains commands that will be automatically executed by cron.

Now, simply enter the full path of the scrpt that you created.

💡
For example: We created the bash script clean_temp.sh in /home directory, so the cron job that you need to setup on the crontab file should look like below

0 0 * * 0 /home/ubuntu_server/clean_temp.sh

💡
Note: 0 0 * * 0 means that the bash script will run once a week. You can set any time interval you like by using crontab expression.

Once you have added the command to run clean_temp.sh in crontab file, you need to save changes by pressing CTRL + X first

then press Y key

and finally press enter to write changes to the file.

That's it! Now the bash script will be executed by cron automatically depending the time interval you set.

Conclusion

In this tutorial, you learned how to clean temporary files on Ubuntu, by using rm and find commands. We also explained how you can delete temporary files automatically via cron and bash script.