We are decent with scripting language bash and not now hot to remove older files for example 1 day. rm
command do not have this type of option to use easily. I think adding this type of feature make a lot of system administrators more happy.
Remove Older Than 1 Day
We will use following find
command example which is explained in detail below.
$ find /tmp -mtime +1 -type f -name '*.tmp' -delete
- find is the command we use
- /tmp is the directory where files resides
- -mtime provide time option and +1 means older then one day
- -type provides file types here we set file but we can use directory, socket etc
- –name ‘*.tmp’ filters files according to their names. we just want tmp extension files
- And the magic resides here -delete will delete the files matching provided criteria
Remove Older Than 1 Week
We can use the same script in order to remove files with extension tmp
from /tmp
directory with the following script. As a week is 7 days we can use +7
to specify a week like below.
$ find /tmp -mtime +7 -type f -name '*.tmp' -delete
Remove Older Than 1 Month
We can use the same script in order to remove files with extension tmp
from /tmp
directory with the following script. As a month is 30 days we can use +30
to specify a week like below.
$ find /tmp -mtime +30 -type f -name '*.tmp' -delete
How To Remove Files Older Than 1 Day/1 Week/1 Month Recursively Infografic
