Delete all files and folders in /tmp older than a day

Solution 1:

First, use find to select these files:

find /tmp -mmin +1440

will find files that were modified more than 1440 minutes ago. (There is an option to use days instead of minutes, but it rounds upwards and +1 will mean 2 days or more, unfortunately. See notice below).

Try this, and if you are satisfied that this finds the right files, delete them in one go:

find /tmp -mmin +1440 -delete

See man find for other possibilities (last status changed time, access time).


Notice on the usage of -mtime +1:

In man find

It says:

-mtime n

File's data was last modified n*24 hours ago.

But it also says:

See the comments for -atime to understand how rounding affects the interpretation of file modification times.

The comments for -atime say:

When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so to match -atime +1, a file has to have been accessed at least two days ago.

In other words, -mtime can count only in units of 24 hours or one day each so as far as -mtime +1 goes, this means exactly more than one day by at least one day ( ie. two days+ )

-mmin on the other hand can count in minutes. So, if strict accuracy is vital, then -mmin +1440 ( 1440 minutes = 1 day ) could be used instead of -mtime +1