Coder's Cat

Command Line to List Size of Directory (MacOS/Linux)



I found the command du is different on Mac compared with Linux. A little bit of confusion.

Here is a note how to use du on different OS.

du on Mac

List the size of current directory, the option -h is displaying in human friendly format, the option -s is not recursive to display all sub-directories:

du -hs .

List all the size of sub-directories in current directory:

find . -maxdepth 1 -mindepth 1 -type d -exec du -hs {} \;

If you want to sort according to the size, use pipeline and sort:

find . -maxdepth 1 -mindepth 1 -type d | xargs du -hs | sort -hr

If you want to know which directory cost most disk space, another handy util command line is ncdu. You could install it via homebrew:

brew install ncdu

# Run in the specific directory

The result is like this:


du on Linux

Linux du have the option –max-depth to limit the depth:

du -h --max-depth=1 | sort -hr

Join my Email List for more insights, It's Free!😋

Tags: Misc