blob: a5e581a08d580f65834c84c8b63945410c3f9757 (
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
|
#!/bin/sh
# author: Ehsan Ghorbannezhad <ehsangn@protonmail.ch>
# brief: remove/trash old files/directories from /tmp.
#
# this script removes/trashes top-level files in /tmp that are
# older than the specified amount of time.
# it also removes top-level directories in /tmp that
# are older than the specified amount of time, and don't
# contain any files newer than that time.
# clean files older than how many days:
trash_older_than=30
purge_older_than=90
# paths
tmp=/tmp
trash=/tmp/OLD_TMP_FILES
log=/var/log/tmpcleaner.log
# add typical bin dirs to PATH
export PATH="${PATH}:/usr/bin:/usr/local/bin"
# run with root priviliges
if [ "$1" != -s ]; then
sudo tmpcleaner -s
exit
fi
main()
{
mkdir -p "$trash" || exit 1
must_trash="$(getold $tmp $trash_older_than)"
must_purge="$(getold $trash $purge_older_than)"
echo "$must_trash" | trash
echo "$must_purge" | purge
summary
log
}
trash()
{
while IFS= read -r file; do
[ -e "$file" ] && mv -fv "$file" "$trash"
done
}
purge()
{
while IFS= read -r file; do
[ -e "$file" ] && rm -rfv "$file"
done
}
getold()
{
find "$1" -mindepth 1 -maxdepth 1 -type f -mtime +$2
find "$1" -mindepth 1 -maxdepth 1 -type d -mtime +$2 -not -path "*${trash}*" |
while IFS= read -r dir; do
test "$(find "$dir" -type f -mtime -$2 -quit)" || printf '%s\n' "$dir"
done
}
log()
{
date "+%F %T" >> "$log"
summary | tr '\n' ' ' >> "$log"
}
summary()
{
echo "moved $(line_count "$must_trash") tmpfiles to trash ($trash)."
echo "purged $(line_count "$must_purge") tmpfiles from trash."
}
line_count()
{
[ -z "$1" ] && echo 0 && return
echo "$1" | wc -l
}
main "$@"
exit
|