(Ubuntu Server)

I have a folder full of files in pairs (one is a log, another is a DB backup) They are basically create/modified at the same time.

I need a shell script that detects if the folder has more than 28 or 30 files (always will be a pair), it deletes the oldest two.

What is the quickest way to do this?

Recommended Answers

All 3 Replies

I think this is two searches and then combine these for your final script.

  1. https://www.google.com/search?q=count+number+of+files+in+directory+bash
  2. https://www.google.com/search?q=delete+oldest+file+in+directory+bash

Actually the second research seems to have something very close to what you were looking for.
Try http://stackoverflow.com/questions/25785/delete-all-but-the-most-recent-x-files-in-bash with x set to 28.

None of these links really help out with the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.