http://qs1969.pair.com?node_id=602142


in reply to How do I find and delete files based on age?

untested Perl one liner:
perl -e 'system "rm -rf $_" if ( -M $_ > 14 ) for ( glob "/var/backups +/repository/DATE_*" );'
Note: this is a deliberately simplistic example. To optimise it to avoid shelling out (using either File::Find or putting the glob in a recursive subroutine) would require more Perl experience than OP suggests.

Update: Newbies will particularly benefit from Learning Perl, Third Edition - Making Easy Things Easy and Hard Things Possible By Randal L. Schwartz & Tom Phoenix.

-M

Free your mind

Replies are listed 'Best First'.
Re^2: How do I find and delete files based on age?
by graq (Curate) on Feb 26, 2007 at 16:28 UTC

    I would also highlight strongly to any 'Newbies', especially those unfamiliar with *nix environments not to run this one liner before making backups.

    Because it deletes things. And things that delete other things should always be tested first

    -=( Graq )=-

      Surely the backup is more likely to go wrong than the one liner! Instead a newbie should only be working unsupervised on a non-production machine and backups should be done daily (and in this case also on demand) by properly-qualified staff for all machines including non-production.

      Moreover, Anybody, not just newbies, can make a mistake and it is rather comforting to have the capability to call your friendly sysadmin to restore the damaged goods back to a previous state if there's no quicker repair available.

      I remember having a misunderstanding in a Q/A system over the first digit in an identifier once and deleting everything BUT the data I was supposed to be deleting. Fortunately, a phone call and ten minutes later it was back to where it was.

      -M

      Free your mind

Re^2: How do I find and delete files based on age?
by exussum0 (Vicar) on Feb 26, 2007 at 17:32 UTC
    Touching on what was replied to you, I'd suggest replacing "rm -rf $_" with "echo rm -rf $_" At least then you can audit it until it works perfectly. ^^

      I don't count myself as a newbie, but I always do a test like you suggest (print plain output) before unleashing a deletion script on my system--even if it's just a script to clean out a temp directory.

      It only takes a moment to make sure that you will be deleting what you think you will be deleting, and it is easily worth the time. How long will it take you to restore your files from backup? You have do backups, right?


      TGI says moo

      Yes, that I do agree with. And "print" if its not a shell-out. I also quite often put an echo in front of the perl -e (update: for long or multiple lines being typed in) to check that I typed what I think I typed before actually running it.

      -M

      Free your mind