Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: How do I find and delete files based on age?

by Moron (Curate)
on Feb 26, 2007 at 15:14 UTC ( #602142=note: print w/replies, xml ) Need Help??

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.


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.


      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.


      Free your mind

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://602142]
[karlgoethebier]: marioroy: Racket ball is considered harmful, like Squash
[marioroy]: Wait till folks see what MCE::Hobo can do in the upcoming release. Multiple instances, nested spawning, 3 levels, doesn't matter. it works on Windiows too.
[Lady_Aleena]: I hate sites that tell me to turn ad-blocker off. They should know better.
[marioroy]: s/Windiows/ Windows/
[Discipulus]: marioroy can you expand that Niagara's fall history?
erix only sees fallen angels
erix if it weren't for bad luck, I'd have no luck at all
Discipulus Windiows the OS of native americans..
[karlgoethebier]: marioroy: Next step: Write the ultimate book. "Parallel Programming With Perl. Yet Another Missing Manual" ;-)
[marioroy]: karlgoethebier I do not do Racket ball anymore. That was from gym class long ago.

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2017-05-29 08:48 GMT
Find Nodes?
    Voting Booth?