This quick blog post demonstrates how to get the size of all directories within a directory or a specific directory in Linux – Unix. It also shows some other little tricks for getting the size of directories.
1. Get Size Directory Linux
The simple command below can get the size of a directory in any Unix distribution. It should also work on macOS when using the terminal.
$ du -sh directory_name
The h flag should make the output human-readable.
2. Get Size of All Directories within a Directory
Change to the targeted directory using the cd command and then type the line below; it will print the size of each directory within the working directory.
$ du -sh ./*
Also, if the total size sum is also desired, the flag c should be added to the previous command.
$ du -csh ./*
Last but not least, in case you want to sort the results by size, all you need to do is to add the use the sort call:
$ du -sh ./* | sort -h