How Do I Free up Disk Space?

My Ubuntu cloud server has left only 900MB of disk space.

I'll just empty the directory /tmp and wondering if there is any other location to clean up.

4

18 Answers

To delete downloaded packages (.deb) already installed (and no longer needed)

sudo apt-get clean

To remove all stored archives in your cache for packages that can not be downloaded anymore (thus packages that are no longer in the repository or that have a newer version in the repository).

sudo apt-get autoclean

To remove unnecessary packages (After uninstalling an app there could be packages you don't need anymore)

sudo apt-get autoremove

To delete old kernel versions

sudo apt-get remove --purge linux-image-X.X.XX-XX-generic

If you don't know which kernel version to remove

dpkg --get-selections | grep linux-image

Source: Limpiando Ubuntu: comandos y programas (actualización) (google translated)

6
  • Show top 10 biggest subdirs in the current dir.

     du -sk * | sort -nr | head -10
    
  • Use File Usage Analyzer (AKA baobab; GNOME based), Filelight or kDirStat (KDE based), to see where the disk space is going visually (ncdu uses a TUI).

  • Check if you have old kernels for deletion

     ls -lh /boot
    
  • Cleaning packages

     sudo apt autoremove
     sudo apt autoclean
    

    see list of all installed packages, sorted by size. If you see something big and don't use it - uninstall it

     dpkg-query -W --showformat='${Installed-Size} ${Package}\n' | sort -nr | less
    
  • Clean unused language files with translations (there are tons of them)

     sudo apt install localepurge
    
  • Check content of /var/tmp/

     du -sh /var/tmp/
    
  • Check also

     man deborphan
    
  • Search for big files:

     find / -type f -size +1024k
    

    or

     find / -size +50000  -exec ls -lahg {} \;
    
  • Big installed packages (part of the package: debian-goodies)

     dpigs
    

    or wajig sizes | tail -30.

  • On systemd: Remove the oldest archived journal files until the disk space they use falls below the specified size

     sudo journalctl --vacuum-size 10M
    
  • Limit Tracker disk usage.

8
sudo apt-get autoremove

That can clean out a lot of guff (old kernels, etc) that have been replaced. You can do a similar thing in Synaptic (load it up and select the status button and then the Auto-removeable option).

6

I just freed up almost 2 gigs by removing old kernels and header files:

use

uname -r

to check your current version, then

dpkg -l linux-image-* linux-headers-*

to see all the old kernels and header files, then

sudo apt-get remove linux-image-<XYZ> linux-headers-<XYZ>

the apt-get remove command supports wildcards, so you can do apt-get remove linux-image 3.0.* linux-headers-3.0.* for example, to get rid of many at once.

Make sure you don't kill remove current kernel of course! And maybe keep one or two old version, just in case... but not 10 or 20!

2

Do not forget to Empty Trash.

Or from command line:

rm -r ~/.local/share/Trash/info/ && rm -r ~/.local/share/Trash/files/  

Or just trash-empty using trash-cli package.

4

Uninstall large packages that you don't use

Some packages can be quite large. A convenient way of finding these is to use dpigs. It's not installed by default but can be found in debian-goodies (GitHub project)

sudo apt-get install debian-goodies

Then:

$ dpigs
419576 texlive-latex-extra-doc
204112 nvidia-319
175463 google-chrome-stable
141058 linux-image-extra-3.11.0-15-generic
113173 libreoffice-core
104822 valgrind
102322 qt4-doc
93337 blender
91105 texlive-pstricks-doc
90517 libboost1.53-dev

There are a few options:

-n, --lines=N
  Display the N largest packages on the system (default 10).
-s, --status=status-file
  Use status-file instead of the default dpkg status file.
-S, --source
  Display the largest source packages of binary packages installed
  on the system.
-H, --human-readable
  Display package sizes in human-readable format (like ls -lh or du -h)
-h, --help
  Display this message.
1

Remove all old kernel versions automatically

Attention: If you've just upgraded the kernel, reboot before deleting the older versions!

Remember to check which kernel you are using type:

uname -r

Then as root:

sudo apt-get remove --purge $(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d')

read that thread to be safe not to remove needed kernels with this command!

I really recommend you the program BleachBit, which cleans everything on Ubuntu. Install it using the following command:

sudo apt install bleachbit

Check for large numbers of log files too:

sudo du -h /var/log

Or as rubo77 points out in commends, you could use the NCurses disk usage tool:

sudo ncdu /var/log
2

Cleaner script

This script will execute the biggest chunks on the console:

  • Clean apt cache
  • Remove config files left from uninstalled .deb packages (happens if you don't use the --purge switch with apt-get)
  • Remove every kernel except the one you are using
  • Empty the trashes of every user(including root)

create a file with this content and give it executable rights:

#!/bin/bash
# Adapted from 71529-ubucleaner.sh - 

OLDCONF=$(dpkg -l|grep "^rc"|awk '{print $2}')
CURKERNEL=$(uname -r|sed 's/-*[a-z]//g'|sed 's/-386//g')
LINUXPKG="linux-(image|headers|ubuntu-modules|restricted-modules)"
METALINUXPKG="linux-(image|headers|restricted-modules)-(generic|i386|server|common|rt|xen)"
OLDKERNELS=$(dpkg -l|awk '{print $2}'|grep -E $LINUXPKG |grep -vE $METALINUXPKG|grep -v $CURKERNEL)
YELLOW="\033[1;33m"; RED="\033[0;31m"; ENDCOLOR="\033[0m"

if [ $USER != root ]; then
  echo -e $RED"Error: must be root! Exiting..."$ENDCOLOR
  exit 0
fi

echo -e $YELLOW"Cleaning apt ..."$ENDCOLOR
aptitude clean
apt-get autoremove
apt-get autoclean

echo -e $YELLOW"Those packages were uninstalled without --purge:"$ENDCOLOR
echo $OLDCONF
#apt-get purge "$OLDCONF"  # fixes the error in the original script
for PKGNAME in $OLDCONF ; do  # a better way to handle errors
  echo -e $YELLOW"Purge package $PKGNAME"
  apt-cache show "$PKGNAME"|grep Description: -A3
  apt-get -y purge "$PKGNAME"
done

echo -e $YELLOW"Removing old kernels..."$ENDCOLOR
echo current kernel you are using:
uname -a
aptitude purge $OLDKERNELS

echo -e $YELLOW"Emptying every trashes..."$ENDCOLOR
rm -rf /home/*/.local/share/Trash/*/** &> /dev/null
rm -rf /root/.local/share/Trash/*/** &> /dev/null

echo -e $YELLOW"Script Finished!"$ENDCOLOR

Adapted from 71529-ubucleaner.sh

Find large folders and packages

Some tools that will help you find large folders and packages:

sudo apt install ncdu debian-goodies deborphan
sudo ncdu -xr /  # lists all folders by size on the console (like the gui `baobab`)
dpigs -H         # shows large packages that you don't use
man deborphan    # finds packages that have no packages depending on them
deborphan --guess-all --libdevel | xargs apt-get -s purge

Localepurge

Use localepurge to uninstall unused languages in your system:

sudo apt install localepurge

Remove old snapd images

LANG=c snap list --all | awk '/disabled/{print $1, $3}' | \
    while read snapname revision; do \
        snap remove "$snapname" --revision="$revision"; \
    done

More here.

Bleachbit

A great graphical GUI tool to fit all needs:

sudo apt install bleachbit

More here.

Remove manpages and documentation

Additionally you can remove manpages and documentation as described in the Ubuntu Wiki:

Create a file /etc/dpkg/dpkg.cfg.d/01_nodoc which specifies the desired filters. Example:

path-exclude /usr/share/doc/*
# if we need to keep copyright files for legal reasons:
# path-include /usr/share/doc/*/copyright
path-exclude /usr/share/man/*
path-exclude /usr/share/groff/*
path-exclude /usr/share/info/*
# lintian stuff is small, but really unnecessary
path-exclude /usr/share/lintian/*
path-exclude /usr/share/linda/*

Remove the same set of files and directories in the project-config's postinst. Example:

echo "Removing documentation..."
# if we need to keep copyright files for legal reasons:
# find /usr/share/doc -depth -type f ! -name copyright | xargs rm || true
# else:
find /usr/share/doc -depth -type f | xargs rm || true
rm -rf /usr/share/man/* /usr/share/groff/* /usr/share/info/* /usr/share/lintian/* /usr/share/linda/* /var/cache/man/*
4

If you use UFW, check the log folder. On one of my machines, UFW was generating 8Gb of logs in a couple of days.

You could also empty the apt cache with:

sudo apt-get clean
1

Try using BleachBit (located at sourceforge). It is a great program. the basic idea is that it quickly frees up disk space and removes a lot of the junk that is hidden in the system. There are about 70 applications that it can recognize and wipe clean. There is also the ability to use it to "wipe" the free disk space. I think of it as CCleaner from windows only for linux.

2

First, there is a tool for listing all big folders and files. Just type 'Baobab' into the launcher. By removing folders and files you don't need you get more space.


Then, there is a program for removing double files. The program is called fslint. Install it by entering

sudo apt-get install fslint

in a Terminal.

or graphically


You can also clear the package cache with

sudo apt-get autoclean
sudo apt-get clean
sudo apt-get autoremove

The fourth step is to remove old Kernel entries. You can do this by installing and opening Synaptic. In Synaptic, search for the old Kernel entries (every Kernel entry that is displayed in GRUB except the newest) and remove it.


One good step is to remove the application cache. Do this by installing and running bleachbit:

sudo apt-get install bleachbit

or graphically

Important: Deleted cache cannot be restored!


The last step is to defragment the file system. Do the following for this:

cd $HOME
git clone git://
cd e2fsprogs
./configure
make

With these commands, you download and compile e2fsprogs. (If you or the community has got a better way to install the program, please edit!)

Also make sure that git-core is installed. If not, execute:

sudo apt-get install git-core 

or using Software Center

Now you can run the program with:

cd $HOME/e2fsprogs/misc
./e4defrag /dev/sda1    #defragment /dev/sda1
./e4defrag -v /dev/sda1 # verbose output
./e4defrag -c /dev/sda1 # see overview of fragmentation status

Replace /dev/sda1 with the file system or folder/file you want. Note that for defragmenting a device (such as HDD) you need root privilegs, but not for your own files.


Source (German): here

2

The /tmp directory should never be cleaned up manually unless absolutely necessary (ie: a closed application didn't clean-up after itself).

You can search where space might be filling up using the 'du' command; usually places of interest are inside /var. To name two usual suspects /var/log and '/var/cache'. Though I've had some users who install a backup utility and never realise that it creates GBs of incremental backups in /var/backup that build up over a week or month to fill the entire disk space.

1

Use ubuntu-tweak to clean.

it cleans all *.deb that are downloaded to installation apps.

1

If you have already deleted a lot of junk but disk space does not seem cleaned just reboot ubuntu. Or if you don't want to reboot just run command like in similar question.

sudo service rsyslog restart

You can try clearing the cache and error reports i had about 5 gigs of such junk when i tried this first. I used stacer. You can do it manually also.

  • install stacer sudo apt install stacer
  • stacer is a gui application so you will know what to do. if you don't, launch stacer then click system cleaner (has broom icon on it) click select all then click the magnifying glass select stuff you want to delete then click broom icon again.

When I need make more free space on servers I use this command. It find all files bigger then 50 MB and "du -h" make better list of files and "sort -n" after pipe make list numericcaly sorted by file size.

find / -size +50M -type f -exec du -h {} \; | sort -n
2
Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest