-
Notifications
You must be signed in to change notification settings - Fork 5
/
cleanup_for_cron.sh
42 lines (40 loc) · 1.62 KB
/
cleanup_for_cron.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash
directory=$1
rp=$2
rm -f ./delcleanup.txt
if [[ -z "$directory" || -z "$rp" ]]
then
echo "Please specify a valid absalute direcotry path and retionsion period!"
exit 1
else
if ! [[ "$directory" =~ ^[[:alnum:]]*$ || "$rp" =~ [^0-9] ]];
then
if [[ -d "$directory" ]];
then
echo "--------------------------------------------------------------"
echo "The below-listed files are older than $rp days under this directory $directory"
echo "--------------------------------------------------------------"
find "$directory" -type f -iname "*" -mtime +"$rp" -exec ls {} \; | tee -a ./delcleanup.txt
echo ""
if [ -s "./delcleanup.txt" ]
then
echo "!...........................WARNING..............................!"
echo "We are going to start deleting these above files within 10sec......"
echo "if you don't need to execute this purging task, please quite this script on here using with [ctrl + c]"
echo "!...........................WARNING..............................!"
sleep 10;
echo ""
for delete in $(cat ./delcleanup.txt); do echo "Deleting $delete"; rm -f $delete ; done;
rm -f ./delcleanup.txt
else
echo "There is no files found with this $rp days retention period!"
rm -f ./delcleanup.txt
fi
else
echo "No such direcotry found!"
fi
else
echo "Please enter a valid path and period!"
exit 1
fi
fi