-
Notifications
You must be signed in to change notification settings - Fork 7
/
cleanup.sh
76 lines (62 loc) · 1.58 KB
/
cleanup.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
#!/bin/bash
# Javier Ferrándiz Fernández - 27/07/2023 - https://github.com/javisys
# We check if the current user is root
if [ "$(id -u)" -ne 0 ];
then
echo "This script must be run by the root user. Please run it with sudo or as root."
exit 1
fi
# Directories and files to be cleaned
tmp_dirs=("/tmp" "/var/tmp")
log_files=("/var/log/syslog" "/var/log/auth.log")
#other_files=("")
# Function to delete old files
delete_old_files() {
local dir="$1"
local days="$2"
find "$dir" -type f -mtime +$days -exec rm -rf {} \;
}
# Clean temporary files
for tmp_dir in "tmp_dirs[@]";
do
if [ -d "$tmp_dir" ];
then
echo "Clearing temporary directory: $tmp_dir"
delete_old_files "temp_dir" 7 # Delete files older than 7 days
fi
done
# Clean up old logs
for log_file in "$log_files[@]";
do
if [ -f "$log_file" ];
then
echo "Cleaning log file: $log_file"
truncate -s 0 "$log_file" # Empty the contents of the file
fi
done
# Delete other files that are not needed
#for other_file in "${other_files[@]}";
#do
# if [ -f "$other_file" ];
# then
# echo "Eliminando archivo innecesario: $other_file"
# rm -f "$other_file"
# fi
#done
uninstall_unused_packages() {
apt-get autoremove --purge
}
clean_package_cache() {
apt-get clean
}
clean_unused_configs() {
deborphan | xargs apt-get -y remove --purge
}
# Running additional cleanup for Debian/Ubuntu
if command -v apt &>/dev/null;
then
uninstall_unused_packages
clean_package_cache
clean_unused_configs
fi
echo "Equipment cleaning completed."