Fast way to display the size of each subdirectory in a directory

command linedisk-usage

I want to check which directories take most disk space, quickly.

I tried du -sh subdir but it took more than 20 seconds on bigger directories.

I'm not sure how to display the size of all the subdirectories in the home directory at once with this method, but I'm afraid that it might take minutes…

Is there a fast way to do this?

I don't need to display the size of files, just directories.

Best Answer

Sample directory

$ ls -aF
./  ../  .asd/  folder1/  folder2/  list  t1  t2  xyz/

To find sizes only for folders, excluding hidden folders:

$ find -type d -name '[!.]*' -exec du -sh {} + 
4.0K    ./folder1
4.0K    ./folder2
8.0K    ./xyz

If you need a total at the end as well:

$ find -type d -name '[!.]*' -exec du -ch {} + 
4.0K    ./folder1
4.0K    ./folder2
8.0K    ./xyz
16K total

To sort the results:

$ find -type d -name '[!.]*' -exec du -sh {} + | sort -h
4.0K    ./folder1
4.0K    ./folder2
8.0K    ./xyz

To reverse the sorting order:

$ find -type d -name '[!.]*' -exec du -sh {} + | sort -hr
8.0K    ./xyz
4.0K    ./folder2
4.0K    ./folder1

If you need with hidden directories as well, remove -name '[!.]*' from find command. I don't know any other command to find size of folders that is faster than du. Use df for file system disk space usage

Use find -maxdepth 1 -type d -name '[!.]*' -exec du -sh {} + to avoid sub-folders showing up

Related Question