-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete_all_files_before_a_given_date
73 lines (55 loc) · 2.03 KB
/
Delete_all_files_before_a_given_date
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
if [ $# -eq 0 ]
then
echo "Usage: ./script.sh directory_path date[optional]"
echo "date should be in proper format e.g. 20180506 for 6th May, 2018"
echo "In the absence of date, all files older than 15 days from today will be deleted in the given directory otherwise all files older than the given date will be deleted"
exit 1
fi
input_directory=$1
echo "input_directory: $input_directory"
days_in_year=365
days_in_month=30
backup_duration_in_days=15
if [ $# -eq 2 ]
then
echo "no. of parameters are 2"
reference_date=$2
else
reference_date=$(date +"%Y%m%d") # = 20130718
fi
##################### Getting reference date in proper format ###########################
mod_reference_date=$(echo $reference_date|awk '{print substr($1,1,4)}')
mod_reference_date=$((mod_reference_date * days_in_year))
temp=$(echo $reference_date|awk '{print substr($1,5,2)}')
temp=${temp#0}
temp=$((temp*days_in_month))
mod_reference_date=$((mod_reference_date+temp))
if [ $# -eq 1 ]
then
mod_reference_date=$((mod_reference_date+temp-backup_duration_in_days))
fi
echo "mod_reference_date: $mod_reference_date"
#################### Function for getting file's last modification date in proper format ####################
file_date()
{
file_date=$(stat $1|awk -F': ' '/Modify: /{print $2}'|awk '{print $1}'|sed 's/-//g') # = 20130715
mod_file_date=$(echo $file_date|awk '{print substr($1,1,4)}')
mod_file_date=$((mod_file_date * days_in_year))
temp=$(echo $file_date|awk '{print substr($1,5,2)}')
temp=$((temp*days_in_month))
temp=${temp#0}
mod_file_date=$((mod_file_date+temp))
temp=$(echo $file_date|awk '{print substr($1,7,2)}')
temp=${temp#0}
mod_file_date=$((mod_file_date+temp))
echo " Inside function $1: $mod_file_date"
if [ $mod_file_date -lt $mod_reference_date ]
then
rm -rf $2/$1
fi
}
for i in $( ls $input_directory);
do
file_date $i $input_directory
done