Du command gives different result for directory size

Hi,

Welcome to It’s FOSS Community forum.

Ah! The things you discover when you start playing with Linux.

There is one simple explanation of this: the hidden files and folders.

When you did sudo du -shc /home/*, it showed 3 directories and their sum.

And when you did sudo du -shc /home/roger/*, it probably showed a bunch of directories and files and their sum.

The difference comes from what it sees. When you do /home/roger/, you are listing the contents of the directory first and then calculating its sum. The problem comes from the display because hidden folders won’t be shown in /home/roger/ (you can see the difference by using the ls command option -a).

Since the hidden folders of your home directory was not in the output of /home/roger/*, their directory size was not counted.

The correct way to check the directory size would be to use it like this:

sudo du -shc /home/roger

This way, you are not displaying the contents of the /home/roger directory. Your system will automatically takes into account all the files and folders (even the hidden ones).

I hope I was able to solve the mystery here :slight_smile:

And if you want some more on du command, here is an article I wrote earlier:

2 Likes