Skip to main content
Welcome guest. | Register | Login | Post

Hunting Down Disk Space Hogs on Linux Command Line

There are many ways to check your disk usage on Linux, but in certain settings and situations you might need to do it using the good old Linux command line utilities. This will mostly apply if you're running a Linux server. Even though you can use your server provider's web and graphical tools to gauge how much disk space you are using in total, figuring out what exactly is using up a lot of space is a whole other ball game.

Here we'll focus on doing it right, and doing it simple, by using three commonly available command line tools: df, du, and ncdu.

  • df - displays the disk usage of whole file systems
  • du - displays the disk space usage of files in a given path, or the whole system
  • ncdu - an ncurses tool for displaying and analysing file and directory sizes

Check Total Used and Available Disk Space

This one is pretty simple to do with df. Just run df -h and you'll get an output like this:

Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda        47G   39G  7.7G  84% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            200M  1.3M  199M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            997M     0  997M   0% /run/shm
none            100M     0  100M   0% /run/user

The most important piece of information is on the first line, showing the root path. In this example we can see it's got a total of 47GB of space of which 39GB is used, 7.7GB is available, which amounts to 84% of the disk space used.

The -h option simply formats the size values in a human friendly form, which is counting it in gigabytes. Without it the above would look like this:

Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/xvda       48635220 40600268   8034952  84% /
none                   4        0         4   0% /sys/fs/cgroup
none              204080     1252    202828   1% /run
none                5120        0      5120   0% /run/lock
none             1020392        0   1020392   0% /run/shm
none              102400        0    102400   0% /run/user

So as you can see those are pretty big numbers, hard to wrap our heads around, and that's because they are counted in mere kilobytes. 47G sure sounds a lot easier to get than 48635220, and chances are the precision of the latter number wont matter in 99% of the cases. If you want it in megabytes run df -m.

Running df is the first step to take when you're running out of disk space and want to see what's taking up space. If you've got multiple partitions it will list them all and tell you which one is getting full.

Then you can drill down to the actual files.

Finding Out What's Taking up All The Space with du

Checking total disk space used is easy business, but figuring out which files exactly are taking up all that space is a little trickier. For this we can use du and ncdu.

We could talk about ncdu right off the bat because it's hands down the easiest method, but let's cover a few useful du commands first that are good to know, and can be quite helpful as well, especially if you don't have ncdu on your system.

If you simply run du it will start listing file sizes of everything under the current directory, in kilobytes. Chances are this will be a bazillion files flying across your screen making it pretty difficult to plow through.

Instead of doing that you can pass some options to make the results far more useful. For example, this command just gives you the total size of all files in the current directory:

du -csh

If you do this in the root directory you get total file size on the file system. To get the same for /home you just run:

du -csh /home

-c produces the grand total
-s skips listing all the files and just shows the summary
-h switches to the human readable format for the values

But when you're looking for the specific files and directories taking up the most space it can be daunting to check each directory like this. Luckily you can use the following command to list total size of all directories within the current one, and sort them by size:

du -ch -d 1 | sort -hr

The output will look something like this:

39G     total
39G     .
25G     ./home
5.2G    ./root
5.1G    ./var
2.4G    ./usr
2.1G    ./etc
36M     ./lib
9.9M    ./sbin
6.7M    ./bin
1.3M    ./run
544K    ./boot
140K    ./opt
88K     ./dev
24K     ./tmp
16K     ./lost+found
12K     ./.config
8.0K    ./srv
8.0K    ./media
4.0K    ./mnt
4.0K    ./initrd
0       ./sys
0       ./proc

As you can see /home takes the most space, /root being second, and so on. Pretty simple. You can then move in to /home and do the same there.

-d 1 option is what limits the listing to only the first level of directories in the current directory. If you want to list two levels just type -d 2 instead, and so on.

The | sign is the pipe sign which takes the result of the previous (du) command and passes it to the command after the pipe (AKA piping).

The sort command takes the output of du -ch d 1 and sorts it. The meanings of options are:

  • -h sorts it by number in a human friendly form
  • -r reverses the order, since the above lists from smallest to biggest by default. Leave "r" out if you actually prefer that.

A couple of other du commands you might find useful allow you to only list the files that are above a certain size or in a specified range of sizes.

For example, if you want to list only files which are bigger than 100 MB run this:

du -ch -t 100m

This will list all of the files in on the current path that are bigger than 100 MB in a human readable form, along with a grand total (-c). If you want to sort it like the previous command just do the piping the same way: du -ch -t 100m | sort -hr

Another way to limit the results by size is to display only files the size of which is in a specified range. For example to list files which are anywhere between 1 GB and 5 GB you can do this:

du -ch /home | grep '[0-5]G'

So with these few commands you're well equipped to relatively quickly hunt down those big bad files and directories on your file system, the ones that are most likely to cause you to run out of space.

Easier Method with ncdu

The ncdu command is a lot easier way to do all of the above. It just saves you from running all those commands while providing you with a friendly view of file and directory sizes by default. Just run ncdu to get the list of sizes of all directories on the current path. Or if /you want to check a specific path, like /var, just run:

ncdu /var

It will scan the directory in a cool semi-graphical (ASCII text graphics) manner and display something like this:

That's a pretty neat view of everything, but the best thing of all is that you can now navigate through these directories to see a similar sorted view. Getting down to the biggest space hogs is a breeze.

If you press the ? sign (on keyboard shift-?) you get the list of available keyboard commands. Arrow key usage is pretty obvious already. A few that are worth mentioning are:

  • d - delete the selected file or directory (use with caution)
  • t - toggle between displaying directories before files or vice versa
  • g - switching between ways of showing sizes; can enable/disable percentages and the graph

Chances are you have ncdu on your system, but if you don't it's available here: http://dev.yorhel.nl/ncdu .

Just be careful when deleting files with ncdu, especially if you run it as root. Sometimes things that make it so easy also make it too easy to screw up.

Happy hunting!

AttachmentSize
ncdu.PNG28.97 KB