Diagnosing Linux disk space usage
Diagnosing Linux disk space usage
Problem: Something is using up a lot of disk space but you can’t figure out what it is. How can we quickly diagnose where that space is being used?
Simple (caution examines every file on the system and therefore can take a long time):
# cd / # du -a | sort -rn | less
The top of the display shows you the largest files and directories. In most situations this will provide you with enough of a hint to locate the offending files. However, sometimes the results are inconclusive. For example, the command above generates a lot of output which can be hard to interpret. Or in the case of an Oracle database server, the log files are scattered among several subdirectories, none of which, on their own look to be a problem.
We therefore need a slightly more advanced version of the du command to isolate which directories are they ones we need to focus on.
# du -max-depth 1 -x -h
35M ./sbin 2.1G ./usr 146M ./var 117M ./lib 16K ./lost+found 8.0K ./opt 837M ./home 1.7M ./tmp 1.3M ./root 120M ./etc 6.6M ./bin 8.0K ./srv 23M ./boot 8.0K ./mnt 3.3G .
This version of the command displays a (-h human readable) summary of space used to a maximum depth (–max-depth) of 1 subdirectory limited to the current file system only (-x).
In other words, it displays the total amount of space being used (on this hard drive only) by each directory below your current directory.
In the example above, you can see that of the 3.3G being used, 2.1G is consumed below the /usr directory. Using the same commands, we can focus on the /usr directory to isolate exactly where the storage is being used.
# cd /usr # du --max-depth 1 -h -x
24M ./sbin 24K ./X11R6 598M ./lib 8.0K ./etc 223M ./bin 56K ./src 8.0K ./games 268K ./local 45M ./include 1.6M ./kerberos 31M ./libexec 1.2G ./share 2.1G .
We see that /usr/share is the largest and we can repeat the process until we’ve found the largest space user. Or we can shortcut the process with a new option for du, “-S”. The “-S” option prevents du from including the totals from the sub-directories in the parent directory total. This helps us isolate exactly which directories contain the files using the most space. Here is an example which shows the top 10 largest directories:
# cd /usr/share # du -Sx | sort -rn | head -n 10