---
slug: "how-to-delete-old-backup-file-simply"
title: "Shell Script to Rotate Backup Files Every Few Days"
description: "I recently realized that I can rotate backup files (by deleting old files) every few days by sharding the sequential day numbers, which increase by one each day, using modulo 10. Here's the method I've been using lately."
url: "https://www.ytyng.com/en/blog/how-to-delete-old-backup-file-simply"
publish_date: "2022-11-27T15:03:04Z"
created: "2022-11-27T15:03:04Z"
updated: "2026-02-27T05:44:56.528Z"
categories: []
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20250711/2065914dc7d645a086323052ec04e57f.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# Shell Script to Rotate Backup Files Every Few Days

Here is the English translation of the provided Japanese blog article:

---

If you need to rotate backup files (delete old files) every few days, you can use:

```sh
find backup-dir/ -mtime +10 | xargs rm
```

or use `logrotate` to achieve this.

Recently, I realized that I can achieve this by sharding the sequential days that increase by one each day using % 10. So, I have been using the following method:

```sh
# The shard number changes daily. Rotates every 10 days.
date_shard=$(( ( $(date +%s) / 86400 ) % 10 ))

backup-command > backup-dir/backup.${date_shard}

echo $(date +"%F %T") Done. shard=${date_shard} >> backup-dir/log.txt
```

It can be implemented simply.

---
