Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using the following command to delete old backup files:

find backups/ -name "bak_*.tbz" -mmin +4800 -exec rm -vf {} \;

The problem: When there are no new files, than all files are deleted. I would likke to keep the newest one in such case.

What is an elegant way to delete old files except the newest one?

share|improve this question
up vote 4 down vote accepted
 find  backups/ -name "bak_*.tbz" -mmin +4800 -type f -printf '%T@ %p\n' | sort -k 1nr |\
      tail -n +2 | cut -d ' ' -f 2 | xargs echo rm {}

This assumes GNU find and will print the modification time and file name, sort this by the mod time, delete the first line (newest file), remove the mod time column and hand this over to xargs for deletion (as stated, it will only echo the delete command, not actually do it).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.