Emergancy Space On Linux Script
Page 1 of 1
Emergancy Space On Linux Script
- 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!
Similar topics
» bleachbit linux disk clean space saver remove uneeded files to make space
» Creating Space In An Emergency On Linux
» Space Fence Space Junk Tracker
» Create Emergancy hard drive space on linux fast
» Linux BASH Script Running Commands and Functions From Bash Script and saving to varables
» Creating Space In An Emergency On Linux
» Space Fence Space Junk Tracker
» Create Emergancy hard drive space on linux fast
» Linux BASH Script Running Commands and Functions From Bash Script and saving to varables
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|