• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Need a backup scipt that deletes all files 2+ days older than current date

Need a script for Linux (Redhat ES 4.0) which runs as a part of cron job. Basically it is a backup script that will only ever have backups for the last 2 days, so that any files with a date stamp older than 2+ days of the current date, in a particular directory or file pattern, will delete these files.

2 Solutions
All you need to do is use this line:
find <dir name> -mtime +2 -exec rm {} \;
find /dirtocheck -mtime +2 | gawk '{print "rm " $0}' | /bin/bash

Finds all file more than 2 days old - creates the string "rm filename", and then gets bash to run this string.

The strength of this is that you can leave it as it is, and require a confirmation from the user in order to delete the file...or, change the action to 'forceably' delete the file (This is what you need for your automated script):

find /dirtocheck -mtime +2 | gawk '{print "rm -f " $0}' | /bin/bash

or move the files to another location:

find /dirtocheck -mtime +2 | gawk '{print "mv " $0 " /backupdir"}' | /bin/bash


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now