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

Posted on 2006-04-27
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
    LVL 2

    Accepted Solution

    All you need to do is use this line:
    find <dir name> -mtime +2 -exec rm {} \;
    LVL 22

    Assisted Solution

    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



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    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,…
    SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now