Unable to get the size of a directory by ls
Question
I run the command unsuccessfully to get the size of directories (files inside belong to the size of a directory)
ls -lSrh
I get only kilobits for the size of directories while their content is more than that.
How can you list the size of directories including their content in the number by ls?
Solution
du -sh directory_name
ls
is going to give you the size of the directory entry, not of the directory contents. Based on my quick skim of man ls
there doesn't appear to be a way to make ls
dive down and calculate the size of the directories (most likely because it would be an expensive operation).
OTHER TIPS
If you want to get directories size, you can use
du -sh
du -hs
is great until you have a big directory.
For example, if you store cache files (for example the filesystem cache store for Rails) or uploaded files, … with a partitioned schema, like "cache/123/456/789/real_files" and you have a lot of them, du
is taking forever, eventually blocking all IO.
I don't have a better solution, but this is to be considered before you run this on production server during peak time.
Building on Sean Bright's answer you can do the following (in BASH)
for each in $(echo */ | sed "s/ /\n/g" | sed "s/\///g"); do du -hs "$each"; done;
This will get a full directory content for all the directories in the current directory.