Candid photograph of a Linux server terminal with a soft olive-green glow against a dark slate background, conveying a calm technical atmosphere.

Step-by-step guides for system administrators — covering command-line basics, web server setup, and preparation material for technical interviews.

Browse Tutorials

Configure cron to clean temporary files safely on Linux

Temporary files accumulate on Linux systems through package managers, web applications, editors, and failed processes. On a workstation this may be little more than clutter, but on a RHEL server or Fedora host it can eventually consume disk space and interrupt services.

A scheduled cleanup job provides a predictable retention policy. The safest approach is to remove only files older than a defined age, keep the search within the intended filesystem, and test the command before placing it in root’s crontab. The examples below suit CentOS Stream, RHEL, Fedora, and similar distributions.

Understand temporary file locations

The /tmp directory is intended for short-lived data and is commonly cleared during boot. However, systems that run continuously can retain files for weeks. /var/tmp is designed for data that should survive a reboot, so it normally needs a longer retention period.

Before deleting anything, inspect the directories and their mount points:

df -h /tmp /var/tmp
find /tmp -xdev -type f -printf '%TY-%Tm-%Td %TH:%TM %p\n' | sort | tail

The -xdev option prevents find from crossing into another mounted filesystem. That matters on production machines where an application, container, or network share may be mounted beneath a temporary directory.

Choose a conservative retention period

A common policy is to remove regular files in /tmp older than seven days and files in /var/tmp older than thirty days. Adjust these values for the applications running on the host. A build server in Sydney may create large temporary archives every few minutes, while a small office workstation in Adelaide may generate very little data.

Use -type f so the first cleanup does not remove directories, sockets, or symbolic links:

find /tmp -xdev -type f -mtime +7 -print
find /var/tmp -xdev -type f -mtime +30 -print

The -print action shows what would be selected without deleting it. Remember that -mtime +7 means files whose age is greater than seven complete 24-hour periods, not simply files created before the seventh calendar day.

Test deletion without risking active files

Temporary files can still be in use. Removing an open file may free its directory entry while the process continues consuming disk space, and deleting application state can cause a service failure. Review the dry-run output and confirm that filenames belong to temporary workloads rather than important scripts or data.

Once the result is understood, replace -print with -delete:

find /tmp -xdev -type f -mtime +7 -delete
find /var/tmp -xdev -type f -mtime +30 -delete

If an application has a known working directory, exclude it with -path ... -prune or clean that location with its own retention rule. Keeping a record of the policy in local administrator notes, such as project maintenance notes, can help when several people manage the same host.

Create a reliable cleanup script

A script is easier to audit than a long crontab line. Create /usr/local/sbin/clean-temporary-files with root ownership and restrictive permissions:

#!/bin/bash
set -u

/usr/bin/find /tmp -xdev -type f -mtime +7 -delete
/usr/bin/find /var/tmp -xdev -type f -mtime +30 -delete

Install it with:

sudo install -o root -g root -m 0750 clean-temporary-files \
  /usr/local/sbin/clean-temporary-files

Absolute command paths make cron execution more predictable because cron supplies a limited environment. On a managed RHEL fleet, this also makes the script clearer during audits and easier to review alongside other Linux administration guides.

Schedule the job with cron

Edit root’s crontab rather than a regular user’s crontab:

sudo crontab -e

To run the cleanup at 3:15 am every Sunday, add:

15 3 * * 0 /usr/local/sbin/clean-temporary-files

Cron uses five time fields: minute, hour, day of month, month, and day of week. Australian servers may run in different time zones, especially across cloud regions such as Sydney, Melbourne, and Perth. Confirm the machine’s configured zone with timedatectl; daylight saving changes can alter the local clock time for a scheduled task.

Capture output and verify execution

A quiet command can hide permission errors or unexpected filesystem changes. Log standard output and errors to a dedicated file:

15 3 * * 0 /usr/local/sbin/clean-temporary-files \
  >> /var/log/clean-temporary-files.log 2>&1

The log itself should be rotated if the script later produces regular output. Check the cron service and recent messages with commands appropriate to the distribution:

systemctl status crond
journalctl -u crond --since "24 hours ago"

On some systems the service is named cron rather than crond. Run the script manually first, then inspect the log after its scheduled execution. A monitoring check should also alert administrators if the filesystem reaches a critical threshold.

Consider systemd timers and service impact

Modern Fedora and RHEL installations often use systemd timers for scheduled maintenance. A timer can express calendar schedules, connect cleanly to journald, and provide status information through systemctl. Before adding a custom job, check whether systemd-tmpfiles already manages the relevant paths:

systemd-tmpfiles --clean
systemd-tmpfiles --cat-config

Do not combine overlapping policies without checking their retention rules. A web server may create caches under /var/cache, while an Nginx deployment can have separate temporary upload or proxy paths; review its Nginx configuration guidance before applying broad deletion rules.

A practical cleanup policy is small, explicit, and reversible: inspect first, delete only old regular files, avoid crossing filesystems, and keep a record of execution. On the target host, run the two find commands with -print, review the output, and then enable the tested script in root’s crontab.