List size of all hidden subdirectories with bash – weird globbing behavior

Posted on

Problem :

I wanted to display some information about hidden subdiretories and a noticed a globbing behavior which I don’t understand.

ls .* gets expanded to the pwd, the parent directory path and all hidden subdirectory paths and then lists the contents of all these directories. That’s fine.

du -s .* on the other hand only seems to get expanded to the pwd and the parent directory path because it lists only the size of these. I don’t understand why its behavior differs from the behavior of ls.

Using du -s .[^.]* solves the practical problem because it lists the size of all hidden directories. I understand that this works but why doesn’t du -s .*?

Solution :

I will rephrase my answer, because I have misread the question somehow.

The argument -s summarizes the size of directories, and it happens to include the size of all directories below it. They don’t get shown at the output.

As the globbing .* matches the current directory (.), all child directory gets suppressed by du.

But it’s not THAT simple. This only happens if the parent folder precedes its child folder in the du‘s argument; then the child folder is not shown.

Probably, at your home folder exist the Desktop, Documents and Downloads folders. At your home folder, try running du -s D*. This must show the sizes of these three folders. Now, try du -s . D* and du -s D* ..

Because the globbing .* shows . before the .hidden folders, they don’t get the chance to appear.

Leave a Reply

Your email address will not be published.