Short Of Disk Space - Find Large Files & Clear Caches

When your hard disk is getting full you might want to know why and what files are the largest. This post describes how to find that out and clean up using the disk usage analyser, package cache cleaning and a fast way to list big files on the terminal using du.

Disk Usage Analyzer – A GUI program

Applications → Accessories → Disk Usage Analyzer

Is the same as running baobab on the terminal.

apt-get clean – Clean the Package Cache

get some space by cleaning the package cache:

echo "Free space [MiB] before cleaning packages cache:"
df -m / | grep -v Filesystem | awk '{print $4}'
sudo apt-get clean
echo "Free space [MiB] after cleaning packages cache:"
df -m / | grep -v Filesystem | awk '{print $4}'

Search for Large Files

Search whole file root for big files. The biggest ones are displayed last. On big machines this can take time as really ALL files are listed and sorted. The size is displayed in MB (-m option):

sudo du -m / -a | sort -n

Investigate Used Storage on a Certain Mountpoint

added February 10, 2011

Search for the big folders in the root directory and print their size in MiB:
(do this if you want to make room in your home partition)

sudo du -m --max-depth=1 -x  / 2>/dev/null

The parameters explained:

  • -m prints the size in MiB instead of bytes (You could use -h alternativeley which prints in "human readable" format)
  • --max-depth=1 tells du to return only the sum for every subdirectory.
  • -x takes care that only a single mount point is evaluated.
  • / will investigate the folders in the root directory
  • 2>/dev/null suppresses messages about unreadable directories (missing permissions) by redirecting error messages to the null device

You could also sort the output for the largest files and folders (numerically: -n) like this:

sudo du -x -m / | sort -n

If you see a large difference between the output of du and df then you might experience the open file descriptor problem. To find out use the command lsof | grep tmp where you might replace tmp with another folder to check.
More on this topic can be found on http://www.cyberciti.biz/tips/freebsd-why-command-df-and-du-reports-different-output.html.

Comments