summarylogtreecommitdiffstats
path: root/cache-cleaner
blob: 21d2e94364022b285e4315fa736713d0eb201693 (plain)
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
#!/bin/bash

# Cache Cleaner for Arch Linux
# AUR package: cache-cleaner

VERSION="1.1.1"

show_help() {
    echo "Cache Cleaner $VERSION"
    echo "Usage: cache-cleaner [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  -p, --pacman     Clean pacman cache only"
    echo "  -a, --aur        Clean AUR helpers cache only"
    echo "  -u, --user       Clean user cache only"
    echo "  -s, --system     Clean system cache only"
    echo "  -t, --temp       Clean temporary files (/tmp)"
    echo "  -A, --all        Clean all caches including /tmp"
    echo "  -v, --version    Show version"
    echo "  -h, --help       Show this help"
    echo ""
}

show_version() {
    echo "Cache Cleaner $VERSION"
}

clean_pacman() {
    echo "🧹 Cleaning pacman cache..."
    if command -v pacman >/dev/null 2>&1; then
        if sudo pacman -Sc --noconfirm 2>/dev/null; then
            echo "✅ Pacman cache cleaned"
        else
            echo "âš ī¸  Pacman cache already clean or no access"
        fi
    else
        echo "❌ pacman not found"
    fi
}

clean_aur() {
    echo "🧹 Cleaning AUR cache..."
    local aur_dirs=(
        "$HOME/.cache/yay"
        "$HOME/.cache/paru"
        "$HOME/.cache/trizen"
    )
    
    local cleaned=0
    for dir in "${aur_dirs[@]}"; do
        if [[ -d "$dir" ]]; then
            rm -rf "$dir"/*
            echo "✅ Cleaned: $dir"
            cleaned=1
        fi
    done
    
    if [[ $cleaned -eq 0 ]]; then
        echo "â„šī¸  No AUR helper caches found"
    fi
}

clean_user_cache() {
    echo "🧹 Cleaning user cache..."
    if [[ -d "$HOME/.cache" ]]; then
        rm -rf "$HOME/.cache"/*
        echo "✅ User cache cleaned"
    else
        echo "â„šī¸  User cache directory not found"
    fi
}

clean_system_cache() {
    echo "🧹 Cleaning system cache..."
    if [[ -d "/var/cache" ]]; then
        sudo find /var/cache -type f -delete 2>/dev/null
        echo "✅ System cache cleaned"
    else
        echo "â„šī¸  System cache directory not found"
    fi
}

clean_temp() {
    echo "🧹 Cleaning temporary files (/tmp)..."
    if [[ -w "/tmp" ]]; then
        find /tmp -type f -mmin +60 -delete 2>/dev/null
        find /tmp -type d -empty -mmin +60 -delete 2>/dev/null
        echo "✅ Temporary files cleaned (files older than 1 hour)"
    else
        echo "❌ No write permission for /tmp"
    fi
}

# Parse arguments
MODE="all"

while [[ $# -gt 0 ]]; do
    case $1 in
        -p|--pacman)
            MODE="pacman"
            shift
            ;;
        -a|--aur)
            MODE="aur"
            shift
            ;;
        -u|--user)
            MODE="user"
            shift
            ;;
        -s|--system)
            MODE="system"
            shift
            ;;
        -t|--temp)
            MODE="temp"
            shift
            ;;
        -A|--all)
            MODE="all"
            shift
            ;;
        -v|--version)
            show_version
            exit 0
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            show_help
            exit 1
            ;;
    esac
done

# Main execution
echo "🚀 Starting cache cleanup..."
echo "Mode: $MODE"
echo ""

case $MODE in
    pacman)
        clean_pacman
        ;;
    aur)
        clean_aur
        ;;
    user)
        clean_user_cache
        ;;
    system)
        clean_system_cache
        ;;
    temp)
        clean_temp
        ;;
    all)
        clean_pacman
        clean_aur
        clean_user_cache
        clean_system_cache
        clean_temp
        ;;
esac

echo ""
echo "✅ Cache cleanup completed!"