Automatically Clear Old /tmp Files in Linux cPanel/WHM

Introduction

Temporary files support system processes but can accumulate and waste disk space. Regular cleanup is key for optimal Linux system performance.

Locate Temporary Files

Check common directories like /tmp or /var/tmp to identify files for cleanup.

Delete Old Temporary Files

Run this command in the terminal to remove files older than a specific number of days:

find /tmp -type f -mtime +<X> -exec rm {} \;

Replace <X> with the number of days (e.g., 7 for files older than one week).

Automate Cleanup with Cron Jobs

To regularly delete temporary files without manual intervention, you can set up a cron job. Here’s how:

Open the crontab configuration file

crontab -e

Add the following line to schedule the cleanup task daily at 3 AM

0 3 * * * find /tmp -type f -mtime +<X> -exec rm {} \;

Replace <X> with the desired number of days.

Save and exit the editor. The cron job will now automatically delete files older than the specified time.

Third-Party Tools

Use tools like BleachBit for an easy and efficient way to clean temporary files.

Conclusion

Deleting old temporary files regularly improves system performance. Automate this process with terminal commands or cron jobs for hassle-free maintenance.