1. Home
  2. Linux
  3. Clean the linux temporary folder

How to clean the Linux temporary folder when it fills up

The temporary folder on a Linux system has a limited amount of space. Typically, the size of the temporary folder isn’t an issue, as it clears upon each reboot. However, if you’re running a Linux system that doesn’t reboot often, the folder can fill up and cause immense problems.

Method 1 – Find command

The find command, which is built into all Linux distributions, isn’t just a robust command-line app for Linux that can find files and folders quickly. It can also be used to promptly delete lots of files from any directory — including the one that houses temporary data.

To use the find command to clean the Linux temporary folder, start by opening up a terminal window by pressing Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. Then, once the command-line window is ready to use, switch from a standard user to the root account by using the su or sudo -s command.

su -

or

sudo -s

Now that you’ve gained root access in the terminal use the find command below, combined with the “-delete” switch to empty all files from of the temporary directory.

find /tmp -type f -delete

By running the command above, the temporary directory is empty of all files. However, folders remain. If you’d prefer to delete absolutely everything, try this find command instead.

find /tmp -exec rm -rf {} +

Method 2 – empty directory with rm

Deleting everything out of the temporary directory is best done with method 1, as it avoids using the rm command a whole bunch. That said, if you haven’t had good luck with Method 1, going this route is the only other option.

The first step in cleaning out the temporary directory using the rm command is to open up a terminal window. You can do this by pressing Ctrl + Alt + T or Ctrl + Alt Shift + T on the keyboard. Once the command-line window is open, gain root access.

su -

or

sudo -s

With the command-line switched over to root access, move to the “/tmp” with the CD command.

cd /tmp

Inside of the temporary directory on your Linux system, run the ls command to view the contents of the folder.

ls

Then, run the rm command with the “-rf” switch and a wildcard symbol *. By using a wildcard with the rm command, the Linux command-line will delete every single file and folder at once. This will save time, and make it so that there is no need to write the rm command over and over for each folder and file in the temporary directory.

rm -rf *

From here, run the ls command once again to see the contents of the temporary directory. Assuming the rm command was successful, nothing will appear. If there is still data in the directory, re-run the command above and try again.

Check when Linux temporary folder is full

The temporary directory is an important location on Linux. Many programs and services use it to store temporary data. Sometimes, an excess of data can quickly fill up the folder.

To prevent the temporary directory from filling up in the future, so that there is no need to resort to clearing it manually, here are some quick ways to check the current data usage of the folder.

Df

The Df command is excellent at checking the size of the temporary directory on Linux, as it’s a built-in command-line tool that comes with all Linux operating systems.

Open up a terminal window by pressing Ctrl + Alt + T or Ctrl + Shift + T on the keyboard. Then, with the command-line window open and ready to go, point the du command at the temporary directory to view how much data it is using in blocks.

df /tmp

Don’t like the blocks readout? Consider combining the df command along with the “h” command-line switch. It’ll replace the block readout into plain megabytes and gigabytes, which is much easier to understand.

df -h /tmp

Du

The df command is excellent at showing a detailed readout of the temporary directory. However, if you’re just looking for a quick rundown of how much space the folder is using, the du command is better to use, as it shows only how much of the temporary directory is occupied (in megabytes,) and nothing more.

sudo du -sh /tmp

Tree

Need a detailed readout of exactly how many files and folders are inside of the temporary directory? Try out the tree command.

Note: tree may already be installed on your Linux system. If it’s not, head over to Pkgs.org and learn how to install it.

tree /tmp

The above command will show a small readout of how many files and folders are currently using the temporary directory. However, it won’t show the size of them. If you’d like to see how much data is in all the files and folders are, try the tree command with “du.”

tree --du -h /tmp

Comments are closed.