[Last Call] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2006-04-27
Medium Priority
Last Modified: 2013-12-16
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.

Question by:msobkowicz

Accepted Solution

guruyaya earned 500 total points
ID: 16559766
All you need to do is use this line:
find <dir name> -mtime +2 -exec rm {} \;
LVL 22

Assisted Solution

pjedmond earned 500 total points
ID: 16607127
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



Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month17 days, 14 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question