Skip to content

Instantly share code, notes, and snippets.

@dillondaudert
Last active July 9, 2020 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dillondaudert/0de5b306c3b70d63c3c34b6c64e36863 to your computer and use it in GitHub Desktop.
Save dillondaudert/0de5b306c3b70d63c3c34b6c64e36863 to your computer and use it in GitHub Desktop.
Delete old files from /tmp at regular intervals using cron

Deleting files from /tmp

In this case, I wanted to delete files from /tmp that weren't being removed automatically because the process that created them took a long time to terminate. The following command deletes all files suffixed in .partd, as well as any files in folders suffixed by .partd (what I wanted - note the -type f flag means files only, so not directories) which were last accessed more than 30 minutes ago. The flag -amin +30 indicates last accessed more than 30 minutes ago, and -delete means delete the files found.

find /tmp/*.partd -amin +30 -type f -delete

Creating a basic cron job

I used cron to run this job every half hour. Cron syntax can be more complicated than this, but for this simple example the line in the crontab file was

0,30 * * * * find /tmp/*.partd -amin +30 -type f -delete >/dev/null 2>&1

The redirection at the end is for the output of the command.

Notes

  • crontab -e creates / edits a crontab file for the current user

Links

How to clean /tmp

Cron Job: Guide 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment