Delete more than x days data in Linux Shell Script | Arkit
Write Your Own Method of Script using the below scenario and comment. Recently i came across this situation where i could not able to delete more than x days data using the find command due to -mtime is not matching.
Delete More than x Days Data
Scenario: Every day from Monday to Friday one directory will be created under /fullbackup/dailybackup/YYYY-MM-DD and it will move same dirctory to its parent directory everyday midnight /fullbackup/archive/, However Saturday, Sunday and Monday directories will move to /fullbackup/archive path every Monday evening.
Directory Names Example:
$ ls -ltrdrwxrwxrwx 2 ravi backupgroup 4096 Dec 29 12:25 2018-12-28
drwxrwxrwx 2 ravi backupgroup 4096 Dec 29 12:25 2018-12-29
Question: I would like to delete directories older than two days from /fullbackup/archive path. How do you do it using any scripting methods?
Problem Statement: I was trying to use find . -type d -mtime +4 -exec rm -rf {} \;. This command does not work because the modified date for all moved directories on Monday is the same. I can’t differentiate the last three directories modified date which is -mtime.
How Do you solve it.?? Write a Shell Script to accomplish this task. Should run through crontab and clear directories older than two days.
Shell Script to Delete More than x Days Data
#!/usr/bin/env bash
set -euo pipefail
# -----------------------------
# Configuration
# -----------------------------
ARCHIVE_DIR="/fullbackup/archive"
LOG_FILE="/scripts/joblog.log"
RETENTION_DAYS=2 # Change X days here
DRY_RUN=false # Set true to test without deleting
# Directory name date format expected:
# Example: 2026-05-01
DATE_FORMAT_REGEX='^[0-9]{4}-[0-9]{2}-[0-9]{2}$'
# -----------------------------
# Logging function
# -----------------------------
log_msg() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $*" >> "$LOG_FILE"
}
# -----------------------------
# Validation
# -----------------------------
if [[ ! -d "$ARCHIVE_DIR" ]]; then
log_msg "ERROR: Archive directory does not exist: $ARCHIVE_DIR"
exit 1
fi
CURRENT_EPOCH=$(date +%s)
RETENTION_SECONDS=$(( RETENTION_DAYS * 24 * 60 * 60 ))
log_msg "Backup cleanup started. Retention: ${RETENTION_DAYS} days. Dry run: ${DRY_RUN}"
# -----------------------------
# Process directories
# -----------------------------
find "$ARCHIVE_DIR" -mindepth 1 -maxdepth 1 -type d -print0 |
while IFS= read -r -d '' DIR_PATH; do
DIR_NAME=$(basename "$DIR_PATH")
# Validate directory name format
if [[ ! "$DIR_NAME" =~ $DATE_FORMAT_REGEX ]]; then
log_msg "Skipping invalid directory name: $DIR_PATH"
continue
fi
# Convert directory name to epoch
if ! DIR_EPOCH=$(date -d "$DIR_NAME 00:00:00" +%s 2>/dev/null); then
log_msg "Skipping invalid date directory: $DIR_PATH"
continue
fi
AGE_SECONDS=$(( CURRENT_EPOCH - DIR_EPOCH ))
AGE_DAYS=$(( AGE_SECONDS / 86400 ))
if (( AGE_SECONDS > RETENTION_SECONDS )); then
if [[ "$DRY_RUN" == true ]]; then
log_msg "DRY RUN: Would delete $DIR_PATH, age: ${AGE_DAYS} days"
else
log_msg "Deleting $DIR_PATH, age: ${AGE_DAYS} days"
rm -rf -- "$DIR_PATH"
fi
else
log_msg "Keeping $DIR_PATH, age: ${AGE_DAYS} days"
fi
done
log_msg "Backup cleanup completed."
Explanation
- ls -ltr line will list all the directories on the full backup path and print 9th column using awk and store output to file
- For loop is using output file and prints one directory name YYYY-MM-DD
- STARTTIME will convert directory name to EPOC time by adding 00:00:00 Hours, Minutes, and Seconds
- ENDTIME prints current date & time in EPOC time format
- echo statement will minus End time – start time and print value into minutes
- COUNT variable is assigned with minutes value
- if statement will validate value is more than 2880 minutes means more than two days of time
- if the value is more than 2880 then rm will delete the directory
Solve the scenario using your own method and post the solution in a comment. Thanks.
Note: Above script works only when the directory name is Year-Month-Date otherwise it will give an error message.
Related Articles
Thanks for Your Wonderful Support and Encouragement
More than 40,000 techies are part of our ARKIT community. Join us today and keep learning Linux, Cloud, Storage, DevOps, and IT technologies.