Finding folder size with du and disk space left with df in Linux

Finding folder size with du and disk space left with df in Linux
2 (40%) 3 votes

This post will explain two commands every linux user need to know. Every Linux user is going to use these at one point in their life, and it is usually for when the shit has hit the fan and your server is out of diskspace and you have to find the source of the problem.

The two programs are:

du

and

df

df

First I will show you how to view free disk space. this one is pretty simple.
the command for this is:

df

Just a little tip, I remember it by thinking that it’s short for something, and always come to the phrase “DiskFree”

There are some nice options you can set, to make the output a little bit more clear.
Example, here is makes the sizes more readable by calculating them into Bytes, Megabytes, Gigabytes and so on..

df -h

This one I usually remember by telling myself that i’m a human, not a machine that easily can calculate the Byte value when running df without the -h option: “DiskFree -human

du

“du” is a bit more complex, but not much.
If you just run the command by typing the following, you get a list of all you folder and subfolders for the current folder:

du

Again a little tip on how I remember “du” is by saying what I want, which is viewing the “DirectoryUsage” – Get it? 🙂

Appending a “-H” to the “du” command gives you are nice output in Bytes, Megabytes and Gigabytes just like with “df”. I recommend always appending -H, it just makes everything a lot more easy to read:

du -h

This command will not only display the directories but also all the files that are present in the current directory. Note that “du” always counts all files and directories in the final size in the last line, but with the “-a” it displays the filenames along with the directory names in the output.

du -a

To get a total, of the directory, all subdirectories and files, put a “-c” on the command and the last line will be the total size. Put “-h” on there too, else you will get it in bytes.

du -c -h

To just get the total size of the current folder, you could also use “-s” (small s), again put on -h to make it readable:

du -s -h

If you want to the total size of the current folder, and the total size of all subfolder displayed, you could use “-S” (capital S), again put on the “-h” to make it readable:

du -S -h

There is also a nice exclude feature. Lets say you have a folder full of mp3 music, and video’s. but you only want to know how much space the video’s a taking up. You could use “–exclude=*.mp3” like you see below to ignore all files ending with “.mp3”. These files will not be includes in the final count. Again I use “-h” to make it readable:

du --exclude=*.mp3 -h

If you only want the total of the current directory, and one level down subdirectory’s but don’t want to see everything below that you could use the “-d 1” option. Again I put in “-h” to make it readable:

du -d 1 -h

the “1” means only 1 in depth.

How is “du” useful to you?

Let’s say your Linux server has run out of disk space, or almost, and you dont know what suddenly is using it all.
This is where this command “du” becomes handy.

You could start out by running the following:

du / -h -d 1 --exclude=/proc --exclude=/run

This would show you the total size of all folder in the root ( / ) folder except for /proc and /run since these are for process info and early boot stuff so they are not important and will likely gives you read error if you try to read them since files are created and deleted almost instantly there.
The output will look similar to this (it might take a little time to execute):
linux_du

Here you see that a couple of folder contains a lot of data. by me /mnt is the biggest, this is because I got a secondary hard disk for storage. /home is the seconds largest folder, so let’s take a look into it by putting the directory path into the command we just used (this time without the /run and /proc excludes, since these are not located in the /home folder):

du /home -h -d 1

du_linux2

This time you see the folder indside /home. Here you see that “/home/ss” is the folder taking up almost all the space used by /home. You can now do the same like the above, but on /home/ss/ instead. I think you get the point now 🙂

I think this is a nice little trick and it has saved me a lot of time and trouble in the past, if you like it, feel free to leave a comment below and rate the post at the top!

Share this:Share on FacebookShare on Google+Tweet about this on TwitterShare on Reddit

One thought on “Finding folder size with du and disk space left with df in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *