PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Emergancy Space On Linux Script

Go down

Emergancy Space On Linux Script Empty Emergancy Space On Linux Script

Post by jamied_uk 7th February 2023, 15:29

Code:
#!/bin/bash
# (c) J~Net 2023 - 2024
# ./emergency_space.sh
#
# Script to reclaim disk space on a Raspberry Pi running Raspbian OS.

# Function to check disk space usage
function checkspace(){
    df -h / | awk 'NR==2 {gsub("%","",$5); print $5}'
}

echo "Starting Cleanup..."
echo ""

used1=$(checkspace)
echo "Current Free Space: $used1"

# Truncate log files if they exist
if [ -e /var/log/apache2/error.log ]; then
    sudo truncate -s 0 /var/log/apache2/error.log
fi
if [ -e /var/log/apache2/access.log ]; then
    sudo truncate -s 0 /var/log/apache2/access.log
fi
if [ -e /var/log/auth.log ]; then
    sudo truncate -s 0 /var/log/auth.log
fi
if [ -e /var/log/mail.log ]; then
    sudo truncate -s 0 /var/log/mail.log
fi

# Manage systemd journal
sudo journalctl --rotate
sudo journalctl --vacuum-time=2d
sudo journalctl --vacuum-size=100M

# Clean APT cache and remove unnecessary packages
sudo apt clean
sudo apt autoclean -y && sudo apt autoremove -y

# Remove trash files if the directory exists
if [ -d /root/.local/share/Trash/files ]; then
    sudo find /root/.local/share/Trash/files -mindepth 2 -delete
fi
if [ -d /home/$USER/.local/share/Trash/files ]; then
    sudo find /home/$USER/.local/share/Trash/files -mindepth 2 -delete
fi

# Remove package lists
sudo rm -rf /var/lib/apt/lists/*

# Calculate freed space
used2=$(checkspace)
diff=$((used2-used1))
echo "Freed up $diff% disk space."

echo ""

echo "What log is taking up the most space..."
# Find and list the largest log files
sudo du -a /var/log/* | sort -n -r | head -n 10

echo "Finished Cleanup!"

Note: Backup the logs to network 1st if you need to!
jamied_uk
jamied_uk
Admin

Posts : 2951
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum