Accidentally deleted important files on Linux device?
No worries. Wondershare Recoverit can get back your lost files within 3 steps. The ultimate and professional Linux data recovery software is totally reliable and 100% safe.
Clearing out the trash and temporary file on your system tends to remove unwanted files from your computer while freeing up more space. Every operating system uses a temp folder to store temporary files required for various applications. However, if you run a production server without booting often, this temp folder will grow and cause significant issues for your system. Therefore, it’s best to clear these temp files regularly in Ubuntu.
Part 1. Where Are Temp Files Stored in Ubuntu?
Ubuntu has a temp directory that stores temporary files, usually deleted on each reboot. However, for production servers that don’t restart frequently, the temp folder only gets larger over time and needs to be deleted.
The temporary files in Ubuntu are usually stored in the /tmp directory. You can locate this directory by following the steps below:
- Click on places in the file manager's top menu and then select the Home folder from the list.
- On the left side, navigate to /tmp and choose File System to proceed.
- You can then browse through the directory to get a feel of what it includes.
This is a writable directory, meaning any application or user can create files or directories. However, the system automatically deletes the files when rebooted or not accessed for a certain period.
In addition to the /tmp directory, some applications store their temporary files in user-specific directories like /.cache
or /.local/share.
These directories are commonly used for storing application-specific cache files and other temporary files the application uses.
Part 2. How To Clear Temp Files in Linux/Ubuntu Manually?
Temporary files are created to hold information while a file is being created or modified. After the job is done, they’re supposed to be disposed of because they become obsolete in the manual. After a while, these files begin to take up a lot of space. Cleaning out temporary files is a great way to free up memory on your hard drive. This section will cover your best manual Ubuntu remove temp files methods. However, before clearing your temp files, it’s important to save or back up important files to prevent further issues or having to look for how to recover deleted temp files.
Method 1: Remove Temp Files in Ubuntu Using find
Using the find command is one of the most effective manual methods to clear temp files in Ubuntu Linux. The find
command is prebuilt into every Linux distribution and doesn’t require any form of download, installation, or setup. The find
command also allows you to run other commands, like the rm
command, on the folders and files listed on the result.
This command line isn’t just for quickly finding folders and files; you can also use it to quickly delete several files from any directory, including the one housing temporary data. Follow the steps below to run the find
command to remove temp files in Ubuntu.
Step 1: Open the terminal and run the find command below to find and delete temp files in /tmp.
sudo find /tmp -type f -delete
In the command above, the find
command will locate all files in the /tmp folder, and then users will use the delete option to delete the searched options. However, the /tmp folder will remain.
Step 2: If you want to delete the folder, run the command below.
sudo find /tmp -exec rm -rf {} +
On the other hand, this command will cause the find
command to locate all the files in /tmp and run the rm command on them, deleting both the files and folders simultaneously.
Method 2: Remove Temp Files in Ubuntu Using rm Command
Another option for clearing temp files in Ubuntu is using the rm command. This command manually deletes the files in the /tmp directory. This also means it will delete the folder regardless of whether it contains files. When used with a wildcard, the rm
command deletes files and folders simultaneously.
However, if your goal is to remove the content of the /tmp/ directory alone, then you should use the rm
command with the -rf
switch and a symbol *
in the directory context. This option will cause the rm
command only to delete the files matching the pattern using a wildcard combination.
Step 1: To start, visit the /tmp folder using the cd command.
cd /tmp
Step 2: Once you locate the /tmp folder, you can utilize the rm command to delete its content.
sudo rm -rf *
Step 3: If you need to delete the /tmp folder, run the command below to complete it manually.
sudo rm -rf /tmp
This would eliminate the folder and all its content immediately. The rm
command is powerful, and most times, you can’t recover the files lost. Therefore, you should always back up your tmp files to prevent any issues in the future.
Part 3. How To Clear Temp Files in Ubuntu Linux Automatically?
Apart from the manual method, you can also set up your system to automatically carry out the Linux clear temp files process. This section will cover the best ways to automatically clear temp files in Linux/Ubuntu.
Method 1: Enable Automatically Delete Temporary Files via Settings
Sometimes, you can configure your system to delete temporary files when not in use automatically. This saves you the manually deleting the temp files and improves your PC’s performance. To enable the automatic deletion of temporary files through settings in Ubuntu, follow the steps outlined below.
Step 1: Launch the settings application by clicking the gear icon on the upper right corner of your screen.
Step 2: Once it opens, click on privacy to locate it in the left-hand sidebar.
Step 3: Now scroll through the options provided to the automatic cleaning section and toggle the switch next to ‘Automatically delete temporary files’ to turn it on.
Step 4: It will default delete temporary files saved in the /tmp directory every seven days. You can change the frequency by clicking on ‘Temporary Files’ and choosing different time intervals.
Step 5: If you want to remove some directories or files from the automatic deletion process, there’s a ‘Files and folders to ignore’ list to which you can simply add them.
Step 6: Once you make these changes, close the settings application, and from now on, the system will automatically delete temporary files based on the new settings.
Method 2: Clear Temp Files by Creating Shell Script
If you're looking for an alternative method to automate the deletion of temp folders, you can create a shell script. It's pretty easy to accomplish once you know the proper steps.
Step 1: Open the terminal on your PC and run the command below to create an empty shell script
sudo vi clear_temp.sh
Step 2: Now, add the following lines to it
#!/bin/sh
sudo find /tmp -type f -delete
Step 3: Now, save the file and close it. Run the command below to make it executable.
sudo chmod +x clear_temp.sh
That’s all you have to do; now you can run this shell script to clear the temp folder whenever you want.
./clear_temp.sh
Method 3: Clear Temp Files Using Cronjob
Since you must delete your temp folder regularly, why not automate the process with cronjob? Users can easily set up a cronjob to run a shell script or a specific command quite regularly. To create a cronjob, follow the steps below:
Step 1: Open the terminal on your Linux and run the command below to open cronjobs on your system.
$ crontab -e
Step 2: Next, add the lines below to it to run the find command covered in the first section of this guide. The command below ensures it runs the find command to delete files daily at 10 am automatically.
0 10 * * * sudo find /tmp -type f -delete >/dev/null 2>&1
Step 3: Now, save and close the file. You can always modify the command based on your preference.
Step 4: If you want to set a cronjob to run the shell script above instead of the find command, add the lines below to your command.
0 10 * * * sudo /home/ubuntu/clear_temp.sh >/dev/null 2>&1
Remember to mention the full path to your shell script when using the command above, not the relative path.
Part 4. How To Check If the Temp Folder Is Full
The temporary directory is quite essential on Linux since most services and programs on the system use it to store temporary data. However, the excess data can quickly fill the folder, slowing down the system's performance.
To prevent the temp folder from filling up and causing issues in your Pc, you can check it regularly. By confirming it isn't full, you don't have to clear it up manually. Below are some easy methods to check the status of the temp folder.
First, run the command below to check if the /tmp
folder is full or not
sudo df /tmp
The command above will provide clear information about the amount of space the files occupy. If you want the disk space information and the system to provide it in a human-readable format like GB, MB, KB, etc., then use the -h
option when running the command.
sudo df -h /tmp
If you simply want to know the amount of disk space the /tmp
folder utilizes, then run the command below.
sudo du -sh /tmp
You must run the tree command outlined below for detailed tree-like information about the files and folders in the /tmp
directory.
sudo tree /tmp
You can also run the tree
command with the du
command to list the amount of data contained in the various files and folders in the /tmp
directory. Below is an example for you.
sudo tree -- du -h /tmp
That’s all; now you can check when the /tmp
folder is full, and when it is, manually or automatically carry out the Ubuntu delete temp files process.
Conclusion
In this article, we covered how to locate the /tmp
folder and how to check if it is occupying a lot of space. We also covered the best ways to automatically and manually delete it to free up some space and improve the performance of your folder. This guide allows you to easily create shell scripts and cronjobs to delete the temp folder regularly.