Delete files older than (n) days
Delete Files older Than 30 Days¶
Using the find command, you can search for and delete all files that have been modified more than X days. Also, if required you can delete them with a single command.
First of all, list all files older than 30 days under /opt/backup directory.
find /opt/backup -type f -mtime +30
find /opt/backup -type f -mtime +30 -delete
Delete Files with Specific Extension¶
You can also specify more filters to locate commands rather than deleting all files. For example, you can only delete files with the “.log” extension and modified before 30 days.
For the safe side, first, do a dry run and list files matching the criteria.
find /var/log -name "*.log" -type f -mtime +30
find /var/log -name "*.log" -type f -mtime +30 -delete
Delete Old Directory Recursively¶
The -delete
option may fail if the directory is not empty. In that case, we will use the Linux rm command with find to accomplish the deletion.
Searching all the directories under /var/log modified before 90 days using the command below.
find /var/log -type d -mtime +90
find /var/log -type d -mtime +30 -exec rm -rf {} \;