How to Get the Size of a Directory on Linux

To get the size of a directory, on Linux, we can run this command:

du -sh /path/to/directory/

du is short for "disk usage".

Here's an example. To see the size of the /usr/bin/ directory we would run this command:

du -sh /usr/bin/

Which would return output like this:

In our example, we can see that the /usr/bin/ directory has a size of 110M. Meaning 110 megabytes. The letter K is for kilobytes, M is for megabytes, G is for gigabytes, and T is for terabytes.

So what's with the -sh after the du command? -sh are two command-line options that change the default behavior of du.

Normally, if we'd run du without any options, we'd get something like this:

du /usr/

And the list here would go on, and on.

What is happening is that, by default, du will show us the size of every subdirectory that /usr/ might contain. And that's not what we want, in this case. We just wantdu to show us the size of a single directory, /usr/.

So we can tell du to summarize, with the -s option:

du -s /usr/

Bam! A single line of output instead of the hundreds of lines we got before.

But notice we have another problem. We have to do some math in our heads and convert 2440588. How many megabytes, or gigabytes is that? Maybe not that hard for a small number. But what if the number is 84766284872? It's inconvenient.

To get rid of all this "head math", we can tell du to make the conversions for us. With the -h option du will give us "human readable" numbers, like megabytes, gigabytes, and so on.

So by using du with both options, -s and -h, we get the output we need:

du -sh /usr/

๐Ÿ’ก
Note: When we enter multiple options, we can use the - sign just once, instead of multiple times. Which means that a command like du -s -h can be shortened to du -sh, as we did here.

How to Fix Error: "du: cannot read directory, Permission Denied"

Occasionally, we might run a command like this:

du -sh /etc/

And see that du complains that it can't read some directories.

We notice that it still estimated that this directory is about 6.4 megabytes in size. However, the estimate might be way off if du was not able to read inside many large directories.

This is mainly a permissions issue. Some directories can be "protected". And made readable only by the user called root, or other users.

So with this error du is telling us:

Hey, as the current user I cannot read some of these subdirectories.

How do we fix this? By running the du command as the root user, an administrative account that usually has permissions to read almost anything.

There are two ways to run du as the root user.

1. Prefix the du Command with sudo

If our user has "sudo privileges", we can add sudo in front of our du command.

So du -sh /etc/ becomes:

sudo du -sh /etc/

It will ask for the password of our current user (not root's password). Afterwards, du will run with root privileges. So it should be able to read everything.

If the current user does not have sudo privileges, then we need to log in as the root user directly. To do this we need to either have the password for the root account, or SSH keys set up so that we can log in as root directly (through an SSH login).

2. Log in as the root User, then Run du

To log in as root, type this command:

su --login

We get a prompt to type the password for the root user this time. Not the user we're currently logged in as.

Note that when we type here we get no characters displayed on the screen. That's normal. And we can still use backspace to delete characters if we typed something wrong.

After we log in as root, the prompt will change. After that we can enter our previous du command that couldn't read through the whole directory:

du -sh /etc/

No more "cannot read" errors now, so the estimate should be correct.

At this point, we should log out from the root account, to avoid making accidental changes to our system:

logout

The prompt should switch from root to whatever our user is called on this system:

And that's about it.

If you prefer to inspect directories in a more visual way, see how to use the ncdu command.

Just as a preview, inspecting directory sizes with ncdu looks like this: