Delete Old Files in Linux

In: Linux Tips|Programming

15 Apr 2009

Here’s a good one to have in your bag of tricks: all the time I wind up with a directory where I just want to delete anything older than a certain number of days. Here’s a pretty simple one:


DELETE_PATH='/var/backup/full_dumps/'
DAYS_TO_KEEP='7'

for i in `find $DELETE_PATH -ctime +$DAYS_TO_KEEP`; do
ls -lh "$i"
#rm -vf "$i"
done

The delete is commented out, so it will just list the files. Uncomment when you’re convinced it does what you want. Obviously, change the top two lines to suit your needs, and play around with find (for example, there’s an atime instead of my ctime). But it’s a handy little thing to have around and just adapt as needed.

Related posts:

  1. Keeping Originals after a Delete in Paperclip
  2. vim tricks
  3. vim Tip: Delete all lines which….
  4. Undoing bad tar files
  5. iPhone 4 Antenna

Comment Form

On Other Sites

  • Matt: Hey Victor, A couple good resources for you... http://www.scanboston.com/boston.htm is really det [...]
  • victor: Hi i just got a uniden bearcay scanner and have no local or regional frequency directory.just 1 460 [...]
  • Matt: I do use them periodically. I bought a few i760's, for perhaps $10 apiece in a lot, on eBay a while [...]
  • Marin: Did you eventually end up going with an iDEN phones using Direct Talk? I had some i560's a few year [...]
  • Dan: fyi, EOD = explosive ordnance disposal [...]