From 9a7733d083bb8f4bd0ac5649588716a244cb345e Mon Sep 17 00:00:00 2001 From: simonzhangsz Date: Sat, 28 Oct 2023 16:03:02 +0800 Subject: [PATCH] add: rm expired file shell script --- misc/rmexpfiles.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 misc/rmexpfiles.sh diff --git a/misc/rmexpfiles.sh b/misc/rmexpfiles.sh new file mode 100644 index 00000000..2ba10aab --- /dev/null +++ b/misc/rmexpfiles.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# rm expired file with filename like *20231028111213.zip" + +filepath=$1 +duration=$2 + +find $filepath -maxdepth 1 -type f -name "*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*" -printf "%f\n" | while read filename; do + datestr=$(echo "$filename" | grep -oE '[0-9]{8}') + filedate=$(date -d "$datestr" +%s) + sevendaysago=$(date -d "$duration days ago' +%s) + if [ "$filedate" -lt "$sevendaysago" ]; then + rm -f "$filepath/$filename" + echo "rm file: $filename" + fi +done \ No newline at end of file