Linux: Reclaim Disk Space by Performing System Cleanups

All systems:
Check disk space usage
# check /var directory
du -a /var | sort -nr | head -n 10

# check all directories, filter by large sizes
du -xh / |grep '^\S*[0-9\.]\+G'|sort -rn

# Find the top 20 big files
find / -printf '%s %p\n'| sort -nr | head -20

# Find anything over 100MB
find / -xdev -type f -size +100M -exec ls -la {} \; | sort -nk 5
Debian based systems
# Get rid of unused packages
sudo apt-get autoremove

# Clean apt-cache
sudo apt-get clean

# Clean thumbnail cache
rm -rf ~/.cache/thumbnails/*
Redhat based systems
# Install yum cleaner
yum -y install yum-utils

# Trim log files
find /var -name "*.log" \( \( -size +50M -mtime +7 \) -o -mtime +30 \) -exec truncate {} --size 0 \;

# Clean yum cache
yum clean all
rm -rf /var/tmp/yum-*

# Delete orphanated data from removed repos
rm -rf /var/cache/yum
package-cleanup --quiet --leaves --exclude-bin

# Remove composer cache
rm -rf /root/.composer/cache
rm -rf /home/*/.composer/cache

# Remove core dumps
find -regex ".*/core\.[0-9]+$" -delete

# Remove error logs
find /home/*/public_html/ -name error_log -delete

# If there's NodeJS, clean its cache
rm -rf /root/.npm /home/*/.npm /root/.node-gyp /home/*/.node-gyp /tmp/npm-*
Posted on Categories Linux

Leave a Reply

Your email address will not be published. Required fields are marked *