Red Hat Enterprise Linux (RHEL) 9.x – manage the Linux /tmp directory

It is impossible to force users to comply with the policy of removing files from the ‚/tmp‘ directory in a timely manner so what is a frustrated system administrator to do when you have dozens hundreds or even thousands of ‚/tmp‘ directories and users to deal with the answer is to deploy user file housekeeping

The ‚/tmp‘ directory is a temporary landing place for files and users also have write access to this directory which can be a bad thing but there is a solution

You can create housekeeping scripts and place them in crontab to periodically remove user files from the ‚/tmp‘ directory

This script and schedule ensure that your ‚/tmp‘ directory is kept relatively free of garbage

find /tmp -type f \( ! -user root \) -atime +2

This script displays all files in the ‚/tmp‘ directory not owned by root that have been accessed more than two days ago now to add the command’s removal switch

find /tmp -type f \( ! -user root \) -atime +2 -delete

For example you might add this to your crontab

* */8 * * * /opt/scripts/tmp.clean.sh

Leave a Reply

You must be logged in to post a comment.