How to clean up tmp folder in Linux

The Linux temporary folder ( tmp ) has a limited amount of space but fortunately it is deleted each time the computer is restarted. However, if you don’t reboot your Linux PC often, then problems might arise. For this reason, we at Teknologya have decided to make a tutorial to explain how to clean up the Linux tmp folder using two very simple methods.

How to force shutdown of Linux

How to clean up the Linux tmp directory via find

The find command, integrated into all Linux distributions, allows you to quickly find files and folders and can also be used to promptly delete multiple files from any directory, including the one containing temporary data ( tmp ).

The first thing to do is to open the terminal and switch to the root account with su – o sudo -s. Once this is done, use the command find /tmp -type f -delete to delete all the files in the temporary directory. Alternatively, if you prefer to delete the entire contents (including the folders as well), use find /tmp -exec rm -rf {} +.

How to clean up the Linux tmp directory via rm

The first method allows you to delete the entire contents from the temporary directory and avoids using the entire rm command group. If you have not had luck with it, you will have to use this solution.

As seen with the find command, you need to open the terminal and switch to the root account with su – o sudo -s. Once this is done, move the terminal session to the tmp directory with cd/tmp and then use ls to view the entire contents of the temporary file directory.

In the next step, use rm -rf * to delete each individual file and folder simultaneously. The combination of the -rf option with * will save a lot of time and not necessarily write the rm command several times for each folder and/or file in the tmp directory.

At this point, run the ls command again to view the contents of the temporary folder. If the rm command was executed correctly, you should not see anything. If not, re-run it.

How to check when the Linux tmp folder is full

The tmp folder is used by programs and services to store temporary data. But sometimes the directory can fill up quickly. To avoid this, so as to no longer resort to manual deletion through the terminal, you can use some tools to control data usage.

The df command allows you to verify the size of the temporary directory on Linux in a very simple way. Just open the terminal and use df /tmp to see the amount of data used. Alternatively, you can use df -h /tmp to get information expressed in MB or GB (much easier to understand).

If you are simply looking for a solution to understand the space used by the folder, then we suggest using the command sudo du -sh /tmp. The result of the space occupied by the tmp directory will be expressed in MB.

As an alternative to df and du, you can use tree to get detailed information about files and folders that are inside the tmp folder. In this case, you can use tree /tmp or alternatively tree –du -h /tmp to see in detail also the size of each individual file and/or folder.

Doubts or problems? We help you!

If you still have any doubt or suggestions related to this article then feel free to leave a comment in box below or contact us via our dedicated page.

Leave a Comment