Coder's Cat

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

2020-03-31

file:img/2020_03_31_command-to-list-size-of-directory.org_20200331_194953.png

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
ncdu

The result is like this:

file:img/2020_03_31_command-to-list-size-of-directory.org_20200331_194324.png

du on Linux

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

du -h --max-depth=1 | sort -hr
Tags: Misc