i need a way to delete all the backups that are older than 5 days.

Of course. Dealing with accumulating backups is a common and important task. Here is a straightforward and safe way to automatically delete your backup files that are older than five days using a bash command, perfect for adding to the end of your backup script.

The Command

The most efficient and safest way to do this is with the find command. Here is the one-liner that will do the job:

find /backups -name "backup-sian-*.tgz" -mtime +5 -delete

How It Works

Let’s break down this command so you understand what each part does:

  • find /backups: This initiates the command to find files within the /backups directory. Using the full path is best practice for scripts.
  • -name "backup-sian-*.tgz": This tells find to only target files that match your specific backup naming pattern. The * is a wildcard that matches the date and time strings in your filenames. This is a crucial step to ensure you don’t accidentally delete other files.
  • -mtime +5: This is the core of the cleanup logic. It selects files whose data was last modified more than 5 days ago. This means files that are 6, 7, 8, etc., days old will be selected for deletion. Files from the most recent 5 days will be kept.
  • -delete: This action tells find to delete the files it has found. It’s a safe and efficient way to delete the files directly without needing to pipe to another command like rm.

Important: Perform a Dry Run First

Before you add the -delete action and run the command for real, it is highly recommended that you perform a “dry run” to see a list of the files that will be deleted. This ensures you are removing exactly what you intend to.

To perform a dry run, simply run the command without the -delete action:

find /backups -name "backup-sian-*.tgz" -mtime +5

This will print a list of all the files that are older than five days. Based on your file list and a current date of June 22, 2025, it would list all the backups from June 16, 2025, and earlier. Once you have confirmed that the list is correct, you can proceed with the -delete command.

Adding to Your Backup Script

You can add this command to the end of your backup script. Here is a small script snippet that includes the cleanup command and a confirmation message.

#!/bin/bash

# --- Your existing backup commands would go here ---
# ...
# ...
echo "Backup complete."

# --- Add the cleanup section below ---

echo "Starting cleanup of old backups..."

# Find and delete backups in /backups older than 5 days.
find /backups -name "backup-sian-*.tgz" -mtime +5 -delete

echo "Cleanup complete. Old backups have been removed."

exit 0

By adding this to your script, your backup directory will be automatically maintained each time your backup process runs.