Quantcast
Channel: Eureka! » Shell Script
Viewing all articles
Browse latest Browse all 14

Shell Script – Delete Old Files

$
0
0

For housekeeping the backup files on a server, we can write a shell script to remove files which were created some days ago and scheduled it by cron job.

The following code is an example to remove backup files which were created 90 days ago. Please note that the target folder is just the same as the .sh file location and the .sh file is excluded in the command.

#!/bin/bash
# Add housekeeping which only keeps 90 days backup files

echo "Housekeeping starts"

# Delete backup files which are more than 90days old 
find ./ -mtime +89 -daystart -type f \( ! -iname "*.sh" \) -exec rm "{}" \;

echo "Housekeeping finished"

Enjoy =)


Posted in Shell Script Tagged: Cron Job, Shell Script

Viewing all articles
Browse latest Browse all 14

Trending Articles