Skip to content

Instantly share code, notes, and snippets.

@coalaura
Last active July 21, 2025 15:39
Show Gist options
  • Save coalaura/a75965c40e71371a66387359feceb1a2 to your computer and use it in GitHub Desktop.
Save coalaura/a75965c40e71371a66387359feceb1a2 to your computer and use it in GitHub Desktop.
linux maintenance
function update() {
(
function print_time() {
echo "[$(date '+%H:%M:%S')] - $1"
}
function get_space() {
df --output=used,avail -B1 / | tail -n1
}
read used_before avail_before < <(get_space)
print_time "apt update"
apt-get update -y > /dev/null
print_time "apt full-upgrade"
apt-get full-upgrade -y > /dev/null
print_time "apt autoremove"
apt-get autoremove --purge -y > /dev/null
print_time "apt clean"
apt-get clean -y > /dev/null
if command -v journalctl &> /dev/null; then
print_time "journalctl vacuum"
journalctl --vacuum-time=7d > /dev/null
fi
if command -v docker &> /dev/null; then
print_time "docker builder prune"
docker builder prune --all --force > /dev/null
print_time "docker volume prune"
docker volume prune --all --force > /dev/null
print_time "docker network prune"
docker network prune --force > /dev/null
print_time "docker image prune"
docker image prune --all --force > /dev/null
print_time "docker system prune"
docker system prune --all --force --volumes > /dev/null
for cid in $(docker ps -q); do
print_time "docker clean container $cid..."
docker exec -i "$cid" sh -c "rm -rf /tmp/* /tmp/.[!.]* /tmp/..?* /var/tmp/* /var/tmp/.[!.]* /var/tmp/..?*" > /dev/null
done
fi
print_time "clean old logs"
find /var/log -type f -name "*.log" -mtime +14 -delete > /dev/null
print_time "clean olg /tmp"
find /tmp -type f -atime +7 -delete > /dev/null
print_time "clean olg /var/tmp"
find /var/tmp -type f -atime +7 -delete > /dev/null
read used_after avail_after < <(get_space)
freed=$((used_before - used_after))
freed_human=$(numfmt --to=iec $freed)
print_time "completed, freed: $freed_human"
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment