From 5a41e3596806db431194bcf2dec22ee8bdd7682b Mon Sep 17 00:00:00 2001 From: Klesh Wong Date: Sun, 17 Jan 2021 17:24:48 +0800 Subject: [PATCH] [feature] remove files with name older than N days --- devops/bin/rm-ndays-ago.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100755 devops/bin/rm-ndays-ago.sh diff --git a/devops/bin/rm-ndays-ago.sh b/devops/bin/rm-ndays-ago.sh new file mode 100755 index 0000000..cc96450 --- /dev/null +++ b/devops/bin/rm-ndays-ago.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +set -e + +if [ "$#" -lt 2 ] ;then + echo "Remove files/directories which name contains DatePart and before n days ago" + echo + echo " Usage $0 " + exit 1 +fi + +TARGET_DIR=${1%/} +THRES_DATE=$(date -d "$2 days ago" +%Y%m%d) + +for entry in $(ls "$TARGET_DIR") ;do + DATE=$(echo "$entry" | grep -oP '\b(\d{8})\b') + if [ -n "DATE" ] && [ "$DATE" -lt "$THRES_DATE" ] ;then + rm -f $TARGET_DIR/$entry + fi +done