-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmolt
More file actions
executable file
·175 lines (150 loc) · 4.08 KB
/
molt
File metadata and controls
executable file
·175 lines (150 loc) · 4.08 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
# molt - Clean up old backups and temp files (shed your shell)
# Usage: molt [options]
VERSION="1.0.0"
LOBSTER="🦞"
# Colors
BLUE='\033[0;34m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
show_help() {
cat << EOF
${LOBSTER} molt v${VERSION}
Clean up old files like a lobster shedding its shell.
Usage:
molt [options]
Options:
-p, --path <dir> Directory to clean (default: current)
-o, --older-than <days> Delete files older than N days (default: 30)
-t, --pattern <pattern> File pattern to match (default: *.tar.gz,*.zip,*.bak)
-d, --dry-run Show what would be deleted without deleting
-f, --force Skip confirmation prompt
-h, --help Show this help message
Examples:
molt --older-than 30d
molt --path /backup --older-than 60 --dry-run
molt --pattern "*.log" --older-than 7 --force
Made with ${LOBSTER} by Mullet McNasty
EOF
}
log_info() {
echo -e "${BLUE}${LOBSTER}${NC} $1"
}
log_success() {
echo -e "${GREEN}✅${NC} $1"
}
log_error() {
echo -e "${RED}❌${NC} $1"
}
log_warning() {
echo -e "${YELLOW}⚠️${NC} $1"
}
# Defaults
PATH_TO_CLEAN="."
OLDER_THAN=30
PATTERN="*.tar.gz,*.zip,*.bak"
DRY_RUN=false
FORCE=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-p|--path)
PATH_TO_CLEAN="$2"
shift 2
;;
-o|--older-than)
OLDER_THAN="${2//[^0-9]/}" # Strip non-numeric
shift 2
;;
-t|--pattern)
PATTERN="$2"
shift 2
;;
-d|--dry-run)
DRY_RUN=true
shift
;;
-f|--force)
FORCE=true
shift
;;
*)
log_error "Unknown option: $1"
exit 1
;;
esac
done
# Validate path
if [ ! -d "$PATH_TO_CLEAN" ]; then
log_error "Directory not found: $PATH_TO_CLEAN"
exit 1
fi
log_info "Time to shed some old shells..."
echo -e "${BLUE}Path:${NC} $PATH_TO_CLEAN"
echo -e "${BLUE}Older than:${NC} $OLDER_THAN days"
echo -e "${BLUE}Pattern:${NC} $PATTERN"
if [ "$DRY_RUN" = true ]; then
echo -e "${YELLOW}Mode:${NC} DRY RUN (no files will be deleted)"
fi
echo ""
# Build find command for patterns
FIND_CMD="find \"$PATH_TO_CLEAN\" -type f \\( "
IFS=',' read -ra PATTERNS <<< "$PATTERN"
for i in "${!PATTERNS[@]}"; do
FIND_CMD="$FIND_CMD -name \"${PATTERNS[$i]}\""
if [ $i -lt $((${#PATTERNS[@]} - 1)) ]; then
FIND_CMD="$FIND_CMD -o"
fi
done
FIND_CMD="$FIND_CMD \\) -mtime +$OLDER_THAN"
# Find files
FILES_TO_DELETE=$(eval "$FIND_CMD" 2>/dev/null)
if [ -z "$FILES_TO_DELETE" ]; then
log_success "No old shells found! Everything is fresh."
exit 0
fi
# Count and size
FILE_COUNT=$(echo "$FILES_TO_DELETE" | wc -l)
TOTAL_SIZE=$(echo "$FILES_TO_DELETE" | xargs du -ch 2>/dev/null | tail -1 | cut -f1)
echo -e "${BLUE}Found:${NC} $FILE_COUNT files ($TOTAL_SIZE total)"
echo ""
# Show files
if [ "$DRY_RUN" = true ]; then
echo "$FILES_TO_DELETE" | while read -r file; do
AGE=$(find "$file" -mtime +0 -printf '%Td days old\n' 2>/dev/null | head -1)
echo -e "${YELLOW}Would delete:${NC} $file ${BLUE}($AGE)${NC}"
done
echo ""
log_warning "Dry run complete. Run without --dry-run to actually delete."
exit 0
fi
# Confirm unless forced
if [ "$FORCE" = false ]; then
echo "$FILES_TO_DELETE" | head -5
if [ $FILE_COUNT -gt 5 ]; then
echo -e "${BLUE}... and $((FILE_COUNT - 5)) more${NC}"
fi
echo ""
read -p "🦞 Delete these $FILE_COUNT files? [y/N] " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_info "Molt cancelled. Keeping the old shell."
exit 0
fi
fi
# Delete files
DELETED=0
echo "$FILES_TO_DELETE" | while read -r file; do
if rm "$file" 2>/dev/null; then
((DELETED++))
else
log_warning "Couldn't delete: $file"
fi
done
log_success "Molted successfully! Deleted $FILE_COUNT files ($TOTAL_SIZE freed)."