-
Notifications
You must be signed in to change notification settings - Fork 7
/
clean.sh
executable file
·96 lines (76 loc) · 2.89 KB
/
clean.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
# Copyright (C) Kim Hendrikse, 2023
# Check if required arguments are provided
if [[ $# -ne 4 ]]; then
echo "Usage: $0 -f <from-time> -t <to-time>"
exit 1
fi
# Parse command line arguments
while getopts f:t: flag
do
case "${flag}" in
f) from_time=${OPTARG};;
t) to_time=${OPTARG};;
esac
done
# Convert time frames to date for comparison
if [[ $from_time == *"d" ]]; then
from_time_date=$(date -d"${from_time%d} days ago" +%Y/%Y-%m/%Y-%m-%d_%H-%M-%S.000)
else
from_time_date=$(date -d"${from_time%h} hours ago" +%Y/%Y-%m/%Y-%m-%d_%H-%M-%S.000)
fi
if [[ $to_time == *"d" ]]; then
to_time_date=$(date -d"${to_time%d} days ago" +%Y/%Y-%m/%Y-%m-%d_%H-%M-%S.000)
else
to_time_date=$(date -d"${to_time%h} hours ago" +%Y/%Y-%m/%Y-%m-%d_%H-%M-%S.000)
fi
# Get list of directories for the years
year_dirs=$(find . -maxdepth 1 -type d -name "[0-9][0-9][0-9][0-9]" | sed -e 's/^\.\///' | sort)
# Loop through each year directory
for year_dir in $year_dirs; do
echo "Processing year: $year_dir"
# Check if year is within from_time and to_time
if [[ "$year_dir" < "${from_time_date:0:4}" || "$year_dir" > "${to_time_date:0:4}" ]]; then
continue
fi
# Get list of directories for the months
month_dirs=$(find $year_dir -maxdepth 1 -type d -name "[0-9][0-9][0-9][0-9]-[0-9][0-9]" | sort)
# Loop through each month directory
for month_dir in $month_dirs; do
# Extract the month part between the slashes
month=$(basename $month_dir | cut -d'/' -f2)
# Check if month is within from_time and to_time
if [[ "$month" < "${from_time_date:5:7}" || "$month" > "${to_time_date:5:7}" ]]; then
echo "Skipping month: $month"
continue
fi
echo "Processing month: $month_dir"
# Get list of directories for the days
day_dirs=$(find $month_dir -maxdepth 1 -type d -name "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]" | sort)
# Loop through each day directory
for day_dir in $day_dirs; do
# Check if day is within from_time and to_time
if [[ "$day_dir" < "${from_time_date:0:23}" || "$day_dir" > "${to_time_date:0:23}" ]]; then
continue
fi
echo "Processing day: $day_dir"
for file in $(find $day_dir -type f \( -name "*.flac" -o -name "*.tracking" \) | sort); do
filename_date=$(basename $file)
filename_date=${filename_date:0:23}
if [[ "${filename_date}" < "${from_time_date##*/}" || "${filename_date}" > "${to_time_date##*/}" ]]; then
continue
fi
echo rm $file
rm $file
done
if [ -z "$(ls -A $day_dir)" ] ; then
echo rmdir "$day_dir"
rmdir "$day_dir"
fi
done
if [ -z "$(ls -A $month_dir)" ] ; then
echo rmdir "$month_dir"
rmdir "$month_dir"
fi
done
done