When managing Linux systems, you might notice significant disk space consumption in /usr/lib/modules
and /usr/lib/x86_64-linux-gnu
. These directories often contain:
- Multiple kernel module versions in
/usr/lib/modules
- Architecture-specific shared libraries in
/usr/lib/x86_64-linux-gnu
The apt autoremove
command handles package dependencies but doesn't automatically clean up:
# Check current space usage
du -h -x --max-depth=1 /usr/lib | sort -hr | head -n 5
Output typically shows:
858M /usr/lib/modules
823M /usr/lib/x86_64-linux-gnu
For /usr/lib/modules
, follow these steps:
# List installed kernels
dpkg --list | grep linux-image
# Remove old kernels (keep current and one previous)
sudo apt purge linux-image-5.4.0-XX-generic linux-image-5.4.0-YY-generic
# Alternative method using purge-old-kernels
sudo apt install byobu
sudo purge-old-kernels --keep 2
For /usr/lib/x86_64-linux-gnu
cleanup:
# Find orphaned libraries
sudo deborphan --guess-all | xargs sudo apt-get -s purge
# Remove configuration files
sudo apt-get purge $(deborphan --find-config)
# Clean up residual packages
sudo apt-get autoremove --purge
For more aggressive cleanup:
# Remove unused locales
sudo apt install localepurge
sudo localepurge
# Clean package cache
sudo apt clean
# Remove old configuration files
sudo dpkg --purge $(dpkg --get-selections | grep deinstall | cut -f1)
After performing these operations, verify the results:
df -h /usr
du -sh /usr/lib/modules /usr/lib/x86_64-linux-gnu
Create a maintenance script:
#!/bin/bash
# System cleanup script
echo "Starting system cleanup..."
sudo apt update
sudo apt -y autoremove --purge
sudo apt clean
sudo purge-old-kernels --keep 2 -y
sudo deborphan --guess-all | xargs sudo apt-get -y purge
echo "Cleanup completed. Current disk usage:"
df -h /usr
Save as cleanup.sh
and make executable:
chmod +x cleanup.sh
sudo ./cleanup.sh
After running apt autoremove
, you might notice that /usr/lib/modules
and /usr/lib/x86_64-linux-gnu
still occupy significant disk space. These directories typically contain kernel modules and shared libraries, which can accumulate over time.
First, let's verify which versions are taking up space:
ls -lh /usr/lib/modules
ls -lh /usr/lib/x86_64-linux-gnu | sort -hr | head -n 10
To remove old kernel versions and their modules:
# List installed kernels
dpkg --list | grep linux-image
# Remove specific kernel version (replace x.x.x-x with actual version)
sudo apt purge linux-image-x.x.x-x-generic
# Alternatively, use this to keep only the current and one previous kernel
sudo apt autoremove --purge
For /usr/lib/x86_64-linux-gnu
, be more cautious as these are critical system libraries. However, you can:
# Find orphaned libraries
sudo deborphan --libdevel
# Remove configuration files of removed packages
sudo dpkg --purge dpkg --get-selections | grep deinstall | cut -f1
Install additional tools for more thorough cleaning:
sudo apt install debian-goodies
sudo deborphan --guess-all | xargs sudo apt-get remove --purge
Create a cleanup script:
#!/bin/bash
# Remove old kernels
sudo apt autoremove --purge
# Clean orphaned packages
sudo deborphan --guess-all | xargs sudo apt-get remove --purge
# Clean apt cache
sudo apt clean
# Remove old config files
sudo dpkg --purge $(dpkg --get-selections | grep deinstall | cut -f1)
After cleaning, check the space usage again:
du -h -x --max-depth=1 /usr/lib | sort -hr | head -n 5